aboutsummaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2012-10-02 17:31:49 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2012-10-02 17:31:49 -0400
commit689d99306a1838728075928fcc61ca3ffc112384 (patch)
tree7d8ca33f843125ce5492d0b626a0626a04c1df43 /src/bin
parent87b6a3989fec4a8d10de910671ab702e07aa85cf (diff)
downloadpostgresql-689d99306a1838728075928fcc61ca3ffc112384.tar.gz
postgresql-689d99306a1838728075928fcc61ca3ffc112384.zip
Work around unportable behavior of malloc(0) and realloc(NULL, 0).
On some platforms these functions return NULL, rather than the more common practice of returning a pointer to a zero-sized block of memory. Hack our various wrapper functions to hide the difference by substituting a size request of 1. This is probably not so important for the callers, who should never touch the block anyway if they asked for size 0 --- but it's important for the wrapper functions themselves, which mistakenly treated the NULL result as an out-of-memory failure. This broke at least pg_dump for the case of no user-defined aggregates, as per report from Matthew Carrington. Back-patch to 9.2 to fix the pg_dump issue. Given the lack of previous complaints, it seems likely that there is no live bug in previous releases, even though some of these functions were in place before that.
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/initdb/initdb.c3
-rw-r--r--src/bin/pg_basebackup/streamutil.c3
-rw-r--r--src/bin/pg_ctl/pg_ctl.c3
-rw-r--r--src/bin/pg_dump/dumpmem.c6
-rw-r--r--src/bin/psql/common.c3
-rw-r--r--src/bin/psql/print.c3
6 files changed, 21 insertions, 0 deletions
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 74046b5dfea..a2f16a01c3c 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -285,6 +285,9 @@ pg_malloc(size_t size)
{
void *result;
+ /* Avoid unportable behavior of malloc(0) */
+ if (size == 0)
+ size = 1;
result = malloc(size);
if (!result)
{
diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c
index c32c5acb2b9..e5fd6fd7655 100644
--- a/src/bin/pg_basebackup/streamutil.c
+++ b/src/bin/pg_basebackup/streamutil.c
@@ -54,6 +54,9 @@ xmalloc0(int size)
{
void *result;
+ /* Avoid unportable behavior of malloc(0) */
+ if (size == 0)
+ size = 1;
result = malloc(size);
if (!result)
{
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index 72fc4c1abf6..b288e9a8a63 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -233,6 +233,9 @@ pg_malloc(size_t size)
{
void *result;
+ /* Avoid unportable behavior of malloc(0) */
+ if (size == 0)
+ size = 1;
result = malloc(size);
if (!result)
{
diff --git a/src/bin/pg_dump/dumpmem.c b/src/bin/pg_dump/dumpmem.c
index 3ef10307bc5..cadc89d4447 100644
--- a/src/bin/pg_dump/dumpmem.c
+++ b/src/bin/pg_dump/dumpmem.c
@@ -42,6 +42,9 @@ pg_malloc(size_t size)
{
void *tmp;
+ /* Avoid unportable behavior of malloc(0) */
+ if (size == 0)
+ size = 1;
tmp = malloc(size);
if (!tmp)
exit_horribly(NULL, "out of memory\n");
@@ -64,6 +67,9 @@ pg_realloc(void *ptr, size_t size)
{
void *tmp;
+ /* Avoid unportable behavior of realloc(NULL, 0) */
+ if (ptr == NULL && size == 0)
+ size = 1;
tmp = realloc(ptr, size);
if (!tmp)
exit_horribly(NULL, "out of memory\n");
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 330d5ce12cf..c804148cd57 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -60,6 +60,9 @@ pg_malloc(size_t size)
{
void *tmp;
+ /* Avoid unportable behavior of malloc(0) */
+ if (size == 0)
+ size = 1;
tmp = malloc(size);
if (!tmp)
{
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index 8fa5e371284..6da3ba053e0 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -136,6 +136,9 @@ pg_local_malloc(size_t size)
{
void *tmp;
+ /* Avoid unportable behavior of malloc(0) */
+ if (size == 0)
+ size = 1;
tmp = malloc(size);
if (!tmp)
{