Skip to content

Commit 66cd6a0

Browse files
committed
Currently, contrib/oid2name doesn't bother to free() the memory that it
malloc()'s. This isn't too serious (because oid2name is a short-lived utility, so the memory will soon be returned to the OS on process termination), but I still think it's poor style. This patch changes oid2name so that it allocates memory on the stack where possible and free()s the remaining heap-allocated memory. The patch also fixes a typo a comment and adds 'const' qualifiers to a few 'char *' function parameters. Neil Conway
1 parent a8bd7e1 commit 66cd6a0

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

contrib/oid2name/oid2name.c

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ struct options
4040

4141
/* function prototypes */
4242
void get_opts(int, char **, struct options *);
43-
PGconn *sql_conn(char *, struct options *);
43+
PGconn *sql_conn(const char *, struct options *);
4444
void sql_exec_error(int);
45-
int sql_exec(PGconn *, char *, int);
45+
int sql_exec(PGconn *, const char *, int);
4646
void sql_exec_dumpdb(PGconn *);
4747
void sql_exec_dumptable(PGconn *, int);
48-
void sql_exec_searchtable(PGconn *, char *);
48+
void sql_exec_searchtable(PGconn *, const char *);
4949
void sql_exec_searchoid(PGconn *, int);
5050

5151
/* fuction to parse command line options and check for some usage errors. */
@@ -143,7 +143,6 @@ get_opts(int argc, char **argv, struct options * my_opts)
143143

144144
/* display system tables */
145145
case 'x':
146-
147146
my_opts->systables = 1;
148147
break;
149148

@@ -170,7 +169,7 @@ Usage: pg_oid2name [-d database [-x] ] [-t table | -o oid] \n\
170169

171170
/* establish connection with database. */
172171
PGconn *
173-
sql_conn(char *dbName, struct options * my_opts)
172+
sql_conn(const char *dbName, struct options * my_opts)
174173
{
175174
char *pghost,
176175
*pgport;
@@ -183,11 +182,9 @@ sql_conn(char *dbName, struct options * my_opts)
183182

184183
pghost = NULL;
185184
pgport = NULL;
186-
187185
pgoptions = NULL; /* special options to start up the backend
188186
* server */
189187
pgtty = NULL; /* debugging tty for the backend server */
190-
191188
pguser = NULL;
192189
pgpass = NULL;
193190

@@ -225,12 +222,20 @@ sql_conn(char *dbName, struct options * my_opts)
225222
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
226223
fprintf(stderr, "%s", PQerrorMessage(conn));
227224

228-
229225
PQfinish(conn);
230226
exit(1);
231-
232227
}
233228

229+
/* free data structures: not strictly necessary */
230+
if (pghost != NULL)
231+
free(pghost);
232+
if (pgport != NULL)
233+
free(pgport);
234+
if (pguser != NULL)
235+
free(pguser);
236+
if (pgpass != NULL)
237+
free(pgpass);
238+
234239
/* return the conn if good */
235240
return conn;
236241
}
@@ -266,7 +271,7 @@ sql_exec_error(int error_number)
266271

267272
/* actual code to make call to the database and print the output data */
268273
int
269-
sql_exec(PGconn *conn, char *todo, int match)
274+
sql_exec(PGconn *conn, const char *todo, int match)
270275
{
271276
PGresult *res;
272277

@@ -316,13 +321,11 @@ sql_exec(PGconn *conn, char *todo, int match)
316321
return 0;
317322
}
318323

319-
/* dump all databases know by the system table */
324+
/* dump all databases known by the system table */
320325
void
321326
sql_exec_dumpdb(PGconn *conn)
322327
{
323-
char *todo;
324-
325-
todo = (char *) malloc(1024);
328+
char todo[1024];
326329

327330
/* get the oid and database name from the system pg_database table */
328331
sprintf(todo, "select oid,datname from pg_database");
@@ -335,9 +338,7 @@ sql_exec_dumpdb(PGconn *conn)
335338
void
336339
sql_exec_dumptable(PGconn *conn, int systables)
337340
{
338-
char *todo;
339-
340-
todo = (char *) malloc(1024);
341+
char todo[1024];
341342

342343
/* don't exclude the systables if this is set */
343344
if (systables == 1)
@@ -351,12 +352,10 @@ sql_exec_dumptable(PGconn *conn, int systables)
351352
/* display the oid for a given tablename for whatever db we are connected
352353
to. do we want to allow %bar% in the search? Not now. */
353354
void
354-
sql_exec_searchtable(PGconn *conn, char *tablename)
355+
sql_exec_searchtable(PGconn *conn, const char *tablename)
355356
{
356357
int returnvalue;
357-
char *todo;
358-
359-
todo = (char *) malloc(1024);
358+
char todo[1024];
360359

361360
/* get the oid and tablename where the name matches tablename */
362361
sprintf(todo, "select relfilenode,relname from pg_class where relname = '%s'", tablename);
@@ -376,9 +375,7 @@ void
376375
sql_exec_searchoid(PGconn *conn, int oid)
377376
{
378377
int returnvalue;
379-
char *todo;
380-
381-
todo = (char *) malloc(1024);
378+
char todo[1024];
382379

383380
sprintf(todo, "select relfilenode,relname from pg_class where oid = %i", oid);
384381

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy