pgsql: Work around unportable behavior of malloc(0) and realloc(NULL, 0

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Work around unportable behavior of malloc(0) and realloc(NULL, 0
Date: 2012-10-02 21:32:58
Message-ID: E1TJA5K-0001r6-7k@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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.

Branch
------
REL9_2_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/689d99306a1838728075928fcc61ca3ffc112384

Modified Files
--------------
contrib/oid2name/oid2name.c | 80 +++++++++++++++++++++--------------
contrib/pg_upgrade/util.c | 24 +++++++----
contrib/pgbench/pgbench.c | 6 +++
src/backend/utils/misc/guc.c | 6 +++
src/bin/initdb/initdb.c | 3 +
src/bin/pg_basebackup/streamutil.c | 3 +
src/bin/pg_ctl/pg_ctl.c | 3 +
src/bin/pg_dump/dumpmem.c | 6 +++
src/bin/psql/common.c | 3 +
src/bin/psql/print.c | 3 +
src/port/dirmod.c | 12 ++++-
11 files changed, 106 insertions(+), 43 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Bruce Momjian 2012-10-03 01:18:49 pgsql: In pg_upgrade, use full path name for analyze_new_cluster.sh scr
Previous Message Alvaro Herrera 2012-10-02 21:20:33 pgsql: Refactor "ALTER some-obj SET SCHEMA" implementation