Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit66cd6a0

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-livedutility, so the memory will soon be returned to the OS on processtermination), but I still think it's poor style.This patch changes oid2name so that it allocates memory on the stackwhere possible and free()s the remaining heap-allocated memory. Thepatch also fixes a typo a comment and adds 'const' qualifiers to a few'char *' function parameters.Neil Conway
1 parenta8bd7e1 commit66cd6a0

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
voidget_opts(int,char**,structoptions*);
43-
PGconn*sql_conn(char*,structoptions*);
43+
PGconn*sql_conn(constchar*,structoptions*);
4444
voidsql_exec_error(int);
45-
intsql_exec(PGconn*,char*,int);
45+
intsql_exec(PGconn*,constchar*,int);
4646
voidsql_exec_dumpdb(PGconn*);
4747
voidsql_exec_dumptable(PGconn*,int);
48-
voidsql_exec_searchtable(PGconn*,char*);
48+
voidsql_exec_searchtable(PGconn*,constchar*);
4949
voidsql_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,structoptions*my_opts)
172+
sql_conn(constchar*dbName,structoptions*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
returnconn;
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,intmatch)
274+
sql_exec(PGconn*conn,constchar*todo,intmatch)
270275
{
271276
PGresult*res;
272277

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

319-
/* dump all databasesknow by the system table */
324+
/* dump all databasesknown by the system table */
320325
void
321326
sql_exec_dumpdb(PGconn*conn)
322327
{
323-
char*todo;
324-
325-
todo= (char*)malloc(1024);
328+
chartodo[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,intsystables)
337340
{
338-
char*todo;
339-
340-
todo= (char*)malloc(1024);
341+
chartodo[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,constchar*tablename)
355356
{
356357
intreturnvalue;
357-
char*todo;
358-
359-
todo= (char*)malloc(1024);
358+
chartodo[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,intoid)
377376
{
378377
intreturnvalue;
379-
char*todo;
380-
381-
todo= (char*)malloc(1024);
378+
chartodo[1024];
382379

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp