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

Commite02ef26

Browse files
author
Neil Conway
committed
Fix a potential free() of string literal in oid2name, per report from
Michael Fuhr. Along the way, fix a bunch of accesses to uninitializedmemory, add a mystrdup() routine and use it to cleanup some code.
1 parentb383c1d commite02ef26

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

‎contrib/oid2name/oid2name.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct options
4646
/* function prototypes */
4747
voidget_opts(int,char**,structoptions*);
4848
void*myalloc(size_tsize);
49+
char*mystrdup(constchar*str);
4950
voidadd_one_elt(char*eltname,eary*eary);
5051
char*get_comma_elts(eary*eary);
5152
PGconn*sql_conn(structoptions*);
@@ -68,6 +69,11 @@ get_opts(int argc, char **argv, struct options * my_opts)
6869
my_opts->nodb= false;
6970
my_opts->extended= false;
7071
my_opts->tablespaces= false;
72+
my_opts->dbname=NULL;
73+
my_opts->hostname=NULL;
74+
my_opts->port=NULL;
75+
my_opts->username=NULL;
76+
my_opts->password=NULL;
7177

7278
/* get opts */
7379
while ((c=getopt(argc,argv,"H:p:U:P:d:t:o:f:qSxish?"))!=-1)
@@ -76,8 +82,7 @@ get_opts(int argc, char **argv, struct options * my_opts)
7682
{
7783
/* specify the database */
7884
case'd':
79-
my_opts->dbname= (char*)myalloc(strlen(optarg));
80-
sscanf(optarg,"%s",my_opts->dbname);
85+
my_opts->dbname=mystrdup(optarg);
8186
break;
8287

8388
/* specify one tablename to show */
@@ -102,26 +107,22 @@ get_opts(int argc, char **argv, struct options * my_opts)
102107

103108
/* host to connect to */
104109
case'H':
105-
my_opts->hostname= (char*)myalloc(strlen(optarg));
106-
sscanf(optarg,"%s",my_opts->hostname);
110+
my_opts->hostname=mystrdup(optarg);
107111
break;
108112

109113
/* port to connect to on remote host */
110114
case'p':
111-
my_opts->port= (char*)myalloc(strlen(optarg));
112-
sscanf(optarg,"%s",my_opts->port);
115+
my_opts->port=mystrdup(optarg);
113116
break;
114117

115118
/* username */
116119
case'U':
117-
my_opts->username= (char*)myalloc(strlen(optarg));
118-
sscanf(optarg,"%s",my_opts->username);
120+
my_opts->username=mystrdup(optarg);
119121
break;
120122

121123
/* password */
122124
case'P':
123-
my_opts->password= (char*)myalloc(strlen(optarg));
124-
sscanf(optarg,"%s",my_opts->password);
125+
my_opts->password=mystrdup(optarg);
125126
break;
126127

127128
/* display system tables */
@@ -183,6 +184,18 @@ myalloc(size_t size)
183184
returnptr;
184185
}
185186

187+
char*
188+
mystrdup(constchar*str)
189+
{
190+
char*result=strdup(str);
191+
if (!result)
192+
{
193+
fprintf(stderr,"out of memory");
194+
exit(1);
195+
}
196+
returnresult;
197+
}
198+
186199
/*
187200
* add_one_elt
188201
*
@@ -208,7 +221,7 @@ add_one_elt(char *eltname, eary *eary)
208221
}
209222
}
210223

211-
eary->array[eary->num]=strdup(eltname);
224+
eary->array[eary->num]=mystrdup(eltname);
212225
eary->num++;
213226
}
214227

@@ -227,7 +240,7 @@ get_comma_elts(eary *eary)
227240
inti,length=0;
228241

229242
if (eary->num==0)
230-
return"";
243+
returnmystrdup("");
231244

232245
/*
233246
* PQescapeString wants 2 * length + 1 bytes of breath space. Add two

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp