Fix some bogus direct uses of realloc().
authorTom Lane
Tue, 18 Nov 2014 18:28:13 +0000 (13:28 -0500)
committerTom Lane
Tue, 18 Nov 2014 18:28:13 +0000 (13:28 -0500)
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.

src/bin/pg_dump/parallel.c
src/bin/psql/command.c
src/bin/psql/tab-complete.c

index 6fe59230c5f729de62d4ccb095e43e7049e75440..864c1a6daee972cff8d31d0b158c9cdb7127db61 100644 (file)
@@ -1300,7 +1300,7 @@ readMessageFromPipe(int fd)
        {
            /* could be any number */
            bufsize += 16;
-           msg = (char *) realloc(msg, bufsize);
+           msg = (char *) pg_realloc(msg, bufsize);
        }
    }
 
@@ -1308,7 +1308,7 @@ readMessageFromPipe(int fd)
     * Worker has closed the connection, make sure to clean up before return
     * since we are not returning msg (but did allocate it).
     */
-   free(msg);
+   pg_free(msg);
 
    return NULL;
 }
index bdf569ce35e0b8921937c8a11f8f4789a2957eff..21cddd11ccfbade99acbcaa45002abbe09acc575 100644 (file)
@@ -1112,12 +1112,7 @@ exec_command(const char *cmd,
            while ((opt = psql_scan_slash_option(scan_state,
                                                 OT_NORMAL, NULL, false)))
            {
-               newval = realloc(newval, strlen(newval) + strlen(opt) + 1);
-               if (!newval)
-               {
-                   psql_error("out of memory\n");
-                   exit(EXIT_FAILURE);
-               }
+               newval = pg_realloc(newval, strlen(newval) + strlen(opt) + 1);
                strcat(newval, opt);
                free(opt);
            }
index 50e0ffc4330283a3ee7cf9636f0e982bae41bdbd..7a8778a919ab2d7b7cca11fc5d812ab5472f98e4 100644 (file)
@@ -3820,13 +3820,8 @@ complete_from_variables(char *text, const char *prefix, const char *suffix)
        if (nvars >= maxvars)
        {
            maxvars *= 2;
-           varnames = (char **) realloc(varnames,
-                                        (maxvars + 1) * sizeof(char *));
-           if (!varnames)
-           {
-               psql_error("out of memory\n");
-               exit(EXIT_FAILURE);
-           }
+           varnames = (char **) pg_realloc(varnames,
+                                           (maxvars + 1) * sizeof(char *));
        }
 
        buffer = (char *) pg_malloc(strlen(ptr->name) + overhead);