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

Commit052cc22

Browse files
committed
Fix a bunch of places that called malloc and friends with no NULL check.
Where possible, use palloc or pg_malloc instead; otherwise, insertexplicit NULL checks.Generally speaking, these are places where an actual OOM is quiteunlikely, either because they're in client programs that don'tallocate all that much, or they're very early in process startupso that we'd likely have had a fork() failure instead. Hence,no back-patch, even though this is nominally a bug fix.Michael Paquier, with some adjustments by meDiscussion: <CAB7nPqRu07Ot6iht9i9KRfYLpDaF2ZuUv5y_+72uP23ZAGysRg@mail.gmail.com>
1 parent9daec77 commit052cc22

File tree

12 files changed

+108
-69
lines changed

12 files changed

+108
-69
lines changed

‎contrib/pg_standby/pg_standby.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ main(int argc, char **argv)
632632
}
633633
break;
634634
case't':/* Trigger file */
635-
triggerPath=strdup(optarg);
635+
triggerPath=pg_strdup(optarg);
636636
break;
637637
case'w':/* Max wait time */
638638
maxwaittime=atoi(optarg);

‎contrib/vacuumlo/vacuumlo.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ vacuumlo(const char *database, const struct _param * param)
242242

243243
if (!schema|| !table|| !field)
244244
{
245-
fprintf(stderr,"Out of memory\n");
245+
fprintf(stderr,"%s",PQerrorMessage(conn));
246246
PQclear(res);
247247
PQfinish(conn);
248248
if (schema!=NULL)
@@ -519,7 +519,7 @@ main(int argc, char **argv)
519519
}
520520
break;
521521
case'U':
522-
param.pg_user=strdup(optarg);
522+
param.pg_user=pg_strdup(optarg);
523523
break;
524524
case'w':
525525
param.pg_prompt=TRI_NO;
@@ -534,10 +534,10 @@ main(int argc, char **argv)
534534
fprintf(stderr,"%s: invalid port number: %s\n",progname,optarg);
535535
exit(1);
536536
}
537-
param.pg_port=strdup(optarg);
537+
param.pg_port=pg_strdup(optarg);
538538
break;
539539
case'h':
540-
param.pg_host=strdup(optarg);
540+
param.pg_host=pg_strdup(optarg);
541541
break;
542542
}
543543
}

‎src/backend/bootstrap/bootstrap.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
uint32bootstrap_data_checksum_version=0;/* No checksum */
4848

4949

50-
#defineALLOC(t,c)((t *) calloc((unsigned)(c), sizeof(t)))
50+
#defineALLOC(t,c) \
51+
((t *) MemoryContextAllocZero(TopMemoryContext, (unsigned)(c) * sizeof(t)))
5152

5253
staticvoidCheckerModeMain(void);
5354
staticvoidBootstrapModeMain(void);
@@ -227,7 +228,7 @@ AuxiliaryProcessMain(int argc, char *argv[])
227228
SetConfigOption("shared_buffers",optarg,PGC_POSTMASTER,PGC_S_ARGV);
228229
break;
229230
case'D':
230-
userDoption=strdup(optarg);
231+
userDoption=pstrdup(optarg);
231232
break;
232233
case'd':
233234
{
@@ -1002,13 +1003,8 @@ boot_get_type_io_data(Oid typid,
10021003
staticForm_pg_attribute
10031004
AllocateAttribute(void)
10041005
{
1005-
Form_pg_attributeattribute= (Form_pg_attribute)malloc(ATTRIBUTE_FIXED_PART_SIZE);
1006-
1007-
if (!PointerIsValid(attribute))
1008-
elog(FATAL,"out of memory");
1009-
MemSet(attribute,0,ATTRIBUTE_FIXED_PART_SIZE);
1010-
1011-
returnattribute;
1006+
return (Form_pg_attribute)
1007+
MemoryContextAllocZero(TopMemoryContext,ATTRIBUTE_FIXED_PART_SIZE);
10121008
}
10131009

10141010
/*

‎src/backend/port/dynloader/darwin.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ pg_dlsym(void *handle, char *funcname)
7878
NSSymbolsymbol;
7979
char*symname= (char*)malloc(strlen(funcname)+2);
8080

81+
if (!symname)
82+
returnNULL;
83+
8184
sprintf(symname,"_%s",funcname);
8285
if (NSIsSymbolNameDefined(symname))
8386
{

‎src/backend/utils/misc/ps_status.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ static char **save_argv;
113113
* overwritten during init_ps_display. Also, the physical location of the
114114
* environment strings may be moved, so this should be called before any code
115115
* that might try to hang onto a getenv() result.)
116+
*
117+
* Note that in case of failure this cannot call elog() as that is not
118+
* initialized yet. We rely on write_stderr() instead.
116119
*/
117120
char**
118121
save_ps_display_args(intargc,char**argv)
@@ -163,8 +166,20 @@ save_ps_display_args(int argc, char **argv)
163166
* move the environment out of the way
164167
*/
165168
new_environ= (char**)malloc((i+1)*sizeof(char*));
169+
if (!new_environ)
170+
{
171+
write_stderr("out of memory\n");
172+
exit(1);
173+
}
166174
for (i=0;environ[i]!=NULL;i++)
175+
{
167176
new_environ[i]=strdup(environ[i]);
177+
if (!new_environ[i])
178+
{
179+
write_stderr("out of memory\n");
180+
exit(1);
181+
}
182+
}
168183
new_environ[i]=NULL;
169184
environ=new_environ;
170185
}
@@ -189,8 +204,20 @@ save_ps_display_args(int argc, char **argv)
189204
inti;
190205

191206
new_argv= (char**)malloc((argc+1)*sizeof(char*));
207+
if (!new_argv)
208+
{
209+
write_stderr("out of memory\n");
210+
exit(1);
211+
}
192212
for (i=0;i<argc;i++)
213+
{
193214
new_argv[i]=strdup(argv[i]);
215+
if (!new_argv[i])
216+
{
217+
write_stderr("out of memory\n");
218+
exit(1);
219+
}
220+
}
194221
new_argv[argc]=NULL;
195222

196223
#if defined(__darwin__)

‎src/bin/pg_archivecleanup/pg_archivecleanup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ main(int argc, char **argv)
314314
dryrun= true;
315315
break;
316316
case'x':
317-
additional_ext=strdup(optarg);/* Extension to remove
317+
additional_ext=pg_strdup(optarg);/* Extension to remove
318318
* from xlogfile names */
319319
break;
320320
default:

‎src/bin/psql/command.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,15 +1182,23 @@ exec_command(const char *cmd,
11821182
fflush(stdout);
11831183
}
11841184
result=gets_fromFile(stdin);
1185+
if (!result)
1186+
{
1187+
psql_error("\\%s: could not read value for variable\n",
1188+
cmd);
1189+
success= false;
1190+
}
11851191
}
11861192

1187-
if (!SetVariable(pset.vars,opt,result))
1193+
if (result&&
1194+
!SetVariable(pset.vars,opt,result))
11881195
{
11891196
psql_error("\\%s: error while setting variable\n",cmd);
11901197
success= false;
11911198
}
11921199

1193-
free(result);
1200+
if (result)
1201+
free(result);
11941202
if (prompt_text)
11951203
free(prompt_text);
11961204
free(opt);

‎src/common/exec.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ set_pglocale_pgservice(const char *argv0, const char *app)
553553
charmy_exec_path[MAXPGPATH];
554554
charenv_path[MAXPGPATH+sizeof("PGSYSCONFDIR=")];/* longer than
555555
* PGLOCALEDIR */
556+
char*dup_path;
556557

557558
/* don't set LC_ALL in the backend */
558559
if (strcmp(app,PG_TEXTDOMAIN("postgres"))!=0)
@@ -583,7 +584,9 @@ set_pglocale_pgservice(const char *argv0, const char *app)
583584
/* set for libpq to use */
584585
snprintf(env_path,sizeof(env_path),"PGLOCALEDIR=%s",path);
585586
canonicalize_path(env_path+12);
586-
putenv(strdup(env_path));
587+
dup_path=strdup(env_path);
588+
if (dup_path)
589+
putenv(dup_path);
587590
}
588591
#endif
589592

@@ -594,7 +597,9 @@ set_pglocale_pgservice(const char *argv0, const char *app)
594597
/* set for libpq to use */
595598
snprintf(env_path,sizeof(env_path),"PGSYSCONFDIR=%s",path);
596599
canonicalize_path(env_path+13);
597-
putenv(strdup(env_path));
600+
dup_path=strdup(env_path);
601+
if (dup_path)
602+
putenv(dup_path);
598603
}
599604
}
600605

‎src/test/isolation/isolationtester.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ main(int argc, char **argv)
119119
for (i=0;i<testspec->nsessions;i++)
120120
nallsteps+=testspec->sessions[i]->nsteps;
121121

122-
allsteps=malloc(nallsteps*sizeof(Step*));
122+
allsteps=pg_malloc(nallsteps*sizeof(Step*));
123123

124124
n=0;
125125
for (i=0;i<testspec->nsessions;i++)
@@ -190,7 +190,7 @@ main(int argc, char **argv)
190190
if (PQresultStatus(res)==PGRES_TUPLES_OK)
191191
{
192192
if (PQntuples(res)==1&&PQnfields(res)==1)
193-
backend_pids[i]=strdup(PQgetvalue(res,0,0));
193+
backend_pids[i]=pg_strdup(PQgetvalue(res,0,0));
194194
else
195195
{
196196
fprintf(stderr,"backend pid query returned %d rows and %d columns, expected 1 row and 1 column",
@@ -286,7 +286,7 @@ run_all_permutations(TestSpec *testspec)
286286
for (i=0;i<testspec->nsessions;i++)
287287
nsteps+=testspec->sessions[i]->nsteps;
288288

289-
steps=malloc(sizeof(Step*)*nsteps);
289+
steps=pg_malloc(sizeof(Step*)*nsteps);
290290

291291
/*
292292
* To generate the permutations, we conceptually put the steps of each
@@ -297,7 +297,7 @@ run_all_permutations(TestSpec *testspec)
297297
* A pile is actually just an integer which tells how many steps we've
298298
* already picked from this pile.
299299
*/
300-
piles=malloc(sizeof(int)*testspec->nsessions);
300+
piles=pg_malloc(sizeof(int)*testspec->nsessions);
301301
for (i=0;i<testspec->nsessions;i++)
302302
piles[i]=0;
303303

@@ -345,7 +345,7 @@ run_named_permutations(TestSpec *testspec)
345345
Permutation*p=testspec->permutations[i];
346346
Step**steps;
347347

348-
steps=malloc(p->nsteps*sizeof(Step*));
348+
steps=pg_malloc(p->nsteps*sizeof(Step*));
349349

350350
/* Find all the named steps using the lookup table */
351351
for (j=0;j<p->nsteps;j++)
@@ -476,8 +476,8 @@ run_permutation(TestSpec *testspec, int nsteps, Step **steps)
476476
return;
477477
}
478478

479-
waiting=malloc(sizeof(Step*)*testspec->nsessions);
480-
errorstep=malloc(sizeof(Step*)*testspec->nsessions);
479+
waiting=pg_malloc(sizeof(Step*)*testspec->nsessions);
480+
errorstep=pg_malloc(sizeof(Step*)*testspec->nsessions);
481481

482482
printf("\nstarting permutation:");
483483
for (i=0;i<nsteps;i++)

‎src/test/isolation/specparse.y

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ setup_list:
7373
}
7474
|setup_listsetup
7575
{
76-
$$.elements =realloc($1.elements,
77-
($1.nelements +1) *sizeof(void *));
76+
$$.elements =pg_realloc($1.elements,
77+
($1.nelements +1) *sizeof(void *));
7878
$$.elements[$1.nelements] =$2;
7979
$$.nelements =$1.nelements +1;
8080
}
@@ -97,23 +97,23 @@ opt_teardown:
9797
session_list:
9898
session_listsession
9999
{
100-
$$.elements =realloc($1.elements,
101-
($1.nelements +1) *sizeof(void *));
100+
$$.elements =pg_realloc($1.elements,
101+
($1.nelements +1) *sizeof(void *));
102102
$$.elements[$1.nelements] =$2;
103103
$$.nelements =$1.nelements +1;
104104
}
105105
|session
106106
{
107107
$$.nelements =1;
108-
$$.elements =malloc(sizeof(void *));
108+
$$.elements =pg_malloc(sizeof(void *));
109109
$$.elements[0] =$1;
110110
}
111111
;
112112

113113
session:
114114
SESSIONstring_literalopt_setupstep_listopt_teardown
115115
{
116-
$$ =malloc(sizeof(Session));
116+
$$ =pg_malloc(sizeof(Session));
117117
$$->name =$2;
118118
$$->setupsql =$3;
119119
$$->steps = (Step **)$4.elements;
@@ -125,15 +125,15 @@ session:
125125
step_list:
126126
step_liststep
127127
{
128-
$$.elements =realloc($1.elements,
129-
($1.nelements +1) *sizeof(void *));
128+
$$.elements =pg_realloc($1.elements,
129+
($1.nelements +1) *sizeof(void *));
130130
$$.elements[$1.nelements] =$2;
131131
$$.nelements =$1.nelements +1;
132132
}
133133
|step
134134
{
135135
$$.nelements =1;
136-
$$.elements =malloc(sizeof(void *));
136+
$$.elements =pg_malloc(sizeof(void *));
137137
$$.elements[0] =$1;
138138
}
139139
;
@@ -142,7 +142,7 @@ step_list:
142142
step:
143143
STEPstring_literalsqlblock
144144
{
145-
$$ =malloc(sizeof(Step));
145+
$$ =pg_malloc(sizeof(Step));
146146
$$->name =$2;
147147
$$->sql =$3;
148148
$$->errormsg =NULL;
@@ -164,15 +164,15 @@ opt_permutation_list:
164164
permutation_list:
165165
permutation_listpermutation
166166
{
167-
$$.elements =realloc($1.elements,
168-
($1.nelements +1) *sizeof(void *));
167+
$$.elements =pg_realloc($1.elements,
168+
($1.nelements +1) *sizeof(void *));
169169
$$.elements[$1.nelements] =$2;
170170
$$.nelements =$1.nelements +1;
171171
}
172172
|permutation
173173
{
174174
$$.nelements =1;
175-
$$.elements =malloc(sizeof(void *));
175+
$$.elements =pg_malloc(sizeof(void *));
176176
$$.elements[0] =$1;
177177
}
178178
;
@@ -181,7 +181,7 @@ permutation_list:
181181
permutation:
182182
PERMUTATIONstring_literal_list
183183
{
184-
$$ =malloc(sizeof(Permutation));
184+
$$ =pg_malloc(sizeof(Permutation));
185185
$$->stepnames = (char **)$2.elements;
186186
$$->nsteps =$2.nelements;
187187
}
@@ -190,15 +190,15 @@ permutation:
190190
string_literal_list:
191191
string_literal_liststring_literal
192192
{
193-
$$.elements =realloc($1.elements,
194-
($1.nelements +1) *sizeof(void *));
193+
$$.elements =pg_realloc($1.elements,
194+
($1.nelements +1) *sizeof(void *));
195195
$$.elements[$1.nelements] =$2;
196196
$$.nelements =$1.nelements +1;
197197
}
198198
|string_literal
199199
{
200200
$$.nelements =1;
201-
$$.elements =malloc(sizeof(void *));
201+
$$.elements =pg_malloc(sizeof(void *));
202202
$$.elements[0] =$1;
203203
}
204204
;

‎src/test/isolation/specscanner.l

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ teardown{ return(TEARDOWN); }
5656
}
5757
<qstr>\"{
5858
litbuf[litbufpos] ='\0';
59-
yylval.str =strdup(litbuf);
59+
yylval.str =pg_strdup(litbuf);
6060
BEGIN(INITIAL);
6161
return(string_literal);
6262
}
@@ -72,7 +72,7 @@ teardown{ return(TEARDOWN); }
7272
}
7373
<sql>{space}*"}" {
7474
litbuf[litbufpos] ='\0';
75-
yylval.str =strdup(litbuf);
75+
yylval.str =pg_strdup(litbuf);
7676
BEGIN(INITIAL);
7777
return(sqlblock);
7878
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp