Skip to content

Commit 84e4570

Browse files
committed
Fix set of issues with memory-allocation system calls in frontend code
Like the backend, the frontend has wrappers on top of malloc() and such whose use is recommended. Particularly, it is possible to do memory allocation without issuing an error. Some binaries missed the use of those wrappers, so let's fix the gap for consistency. This also fixes two latent bugs: - In pg_dump/pg_dumpall when parsing an ACL item, on an out-of-memory error for strdup(), the code considered the failure as a ACL parsing problem instead of an actual OOM. - In pg_waldump, an OOM when building the target directory string would cause a crash. Author: Daniel Gustafsson Discussion: https://postgr.es/m/gY0y9xenfoBPc-Tufsr2Zg-MmkrJslm0Tw_CMg4p_j58-k_PXNC0klMdkKQkg61BkXC9_uWo-DcUzfxnHqpkpoR5jjVZrPHqKYikcHIiONhg=@yesql.se
1 parent 34ff542 commit 84e4570

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

src/bin/pg_ctl/pg_ctl.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,8 @@ GetPrivilegesToDelete(HANDLE hToken)
19791979
return NULL;
19801980
}
19811981

1982-
tokenPrivs = (PTOKEN_PRIVILEGES) malloc(length);
1982+
tokenPrivs = (PTOKEN_PRIVILEGES) pg_malloc_extended(length,
1983+
MCXT_ALLOC_NO_OOM);
19831984
if (tokenPrivs == NULL)
19841985
{
19851986
write_stderr(_("%s: out of memory\n"), progname);

src/bin/pg_dump/dumputils.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -481,15 +481,13 @@ parseAclItem(const char *item, const char *type,
481481
char *slpos;
482482
char *pos;
483483

484-
buf = strdup(item);
485-
if (!buf)
486-
return false;
484+
buf = pg_strdup(item);
487485

488486
/* user or group name is string up to = */
489487
eqpos = copyAclUserName(grantee, buf);
490488
if (*eqpos != '=')
491489
{
492-
free(buf);
490+
pg_free(buf);
493491
return false;
494492
}
495493

@@ -501,13 +499,13 @@ parseAclItem(const char *item, const char *type,
501499
slpos = copyAclUserName(grantor, slpos);
502500
if (*slpos != '\0')
503501
{
504-
free(buf);
502+
pg_free(buf);
505503
return false;
506504
}
507505
}
508506
else
509507
{
510-
free(buf);
508+
pg_free(buf);
511509
return false;
512510
}
513511

@@ -617,7 +615,7 @@ do { \
617615
appendPQExpBuffer(privs, "(%s)", subname);
618616
}
619617

620-
free(buf);
618+
pg_free(buf);
621619

622620
return true;
623621
}

src/bin/pg_test_fsync/pg_test_fsync.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ handle_args(int argc, char *argv[])
170170
switch (option)
171171
{
172172
case 'f':
173-
filename = strdup(optarg);
173+
filename = pg_strdup(optarg);
174174
break;
175175

176176
case 's':

src/bin/pg_waldump/pg_waldump.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,15 @@ identify_target_directory(XLogDumpPrivate *private, char *directory,
247247
{
248248
if (search_directory(directory, fname))
249249
{
250-
private->inpath = strdup(directory);
250+
private->inpath = pg_strdup(directory);
251251
return;
252252
}
253253

254254
/* directory / XLOGDIR */
255255
snprintf(fpath, MAXPGPATH, "%s/%s", directory, XLOGDIR);
256256
if (search_directory(fpath, fname))
257257
{
258-
private->inpath = strdup(fpath);
258+
private->inpath = pg_strdup(fpath);
259259
return;
260260
}
261261
}
@@ -266,13 +266,13 @@ identify_target_directory(XLogDumpPrivate *private, char *directory,
266266
/* current directory */
267267
if (search_directory(".", fname))
268268
{
269-
private->inpath = strdup(".");
269+
private->inpath = pg_strdup(".");
270270
return;
271271
}
272272
/* XLOGDIR */
273273
if (search_directory(XLOGDIR, fname))
274274
{
275-
private->inpath = strdup(XLOGDIR);
275+
private->inpath = pg_strdup(XLOGDIR);
276276
return;
277277
}
278278

@@ -283,7 +283,7 @@ identify_target_directory(XLogDumpPrivate *private, char *directory,
283283
snprintf(fpath, MAXPGPATH, "%s/%s", datadir, XLOGDIR);
284284
if (search_directory(fpath, fname))
285285
{
286-
private->inpath = strdup(fpath);
286+
private->inpath = pg_strdup(fpath);
287287
return;
288288
}
289289
}

src/bin/psql/large_obj.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
200200
char *bufptr;
201201
size_t slen = strlen(comment_arg);
202202

203-
cmdbuf = malloc(slen * 2 + 256);
203+
cmdbuf = pg_malloc_extended(slen * 2 + 256, MCXT_ALLOC_NO_OOM);
204204
if (!cmdbuf)
205205
return fail_lo_xact("\\lo_import", own_transaction);
206206
sprintf(cmdbuf, "COMMENT ON LARGE OBJECT %u IS '", loid);

0 commit comments

Comments
 (0)