Skip to content

Commit 8824bae

Browse files
committed
Fix some bogus direct uses of realloc().
pg_dump/parallel.c was using realloc() directly with no error check. While the odds of an actual failure here seem pretty low, Coverity complains about it, so fix by using pg_realloc() instead. While looking for other instances, I noticed a couple of places in psql that hadn't gotten the memo about the availability of pg_realloc. These aren't bugs, since they did have error checks, but verbosely inconsistent code is not a good thing. Back-patch as far as 9.3. 9.2 did not have pg_dump/parallel.c, nor did it have pg_realloc available in all frontend code.
1 parent ab45d90 commit 8824bae

File tree

3 files changed

+5
-15
lines changed

3 files changed

+5
-15
lines changed

src/bin/pg_dump/parallel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,15 +1300,15 @@ readMessageFromPipe(int fd)
13001300
{
13011301
/* could be any number */
13021302
bufsize += 16;
1303-
msg = (char *) realloc(msg, bufsize);
1303+
msg = (char *) pg_realloc(msg, bufsize);
13041304
}
13051305
}
13061306

13071307
/*
13081308
* Worker has closed the connection, make sure to clean up before return
13091309
* since we are not returning msg (but did allocate it).
13101310
*/
1311-
free(msg);
1311+
pg_free(msg);
13121312

13131313
return NULL;
13141314
}

src/bin/psql/command.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,12 +1112,7 @@ exec_command(const char *cmd,
11121112
while ((opt = psql_scan_slash_option(scan_state,
11131113
OT_NORMAL, NULL, false)))
11141114
{
1115-
newval = realloc(newval, strlen(newval) + strlen(opt) + 1);
1116-
if (!newval)
1117-
{
1118-
psql_error("out of memory\n");
1119-
exit(EXIT_FAILURE);
1120-
}
1115+
newval = pg_realloc(newval, strlen(newval) + strlen(opt) + 1);
11211116
strcat(newval, opt);
11221117
free(opt);
11231118
}

src/bin/psql/tab-complete.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3820,13 +3820,8 @@ complete_from_variables(char *text, const char *prefix, const char *suffix)
38203820
if (nvars >= maxvars)
38213821
{
38223822
maxvars *= 2;
3823-
varnames = (char **) realloc(varnames,
3824-
(maxvars + 1) * sizeof(char *));
3825-
if (!varnames)
3826-
{
3827-
psql_error("out of memory\n");
3828-
exit(EXIT_FAILURE);
3829-
}
3823+
varnames = (char **) pg_realloc(varnames,
3824+
(maxvars + 1) * sizeof(char *));
38303825
}
38313826

38323827
buffer = (char *) pg_malloc(strlen(ptr->name) + overhead);

0 commit comments

Comments
 (0)