summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2012-10-02 17:31:40 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2012-10-02 17:32:42 -0400
commit09ac603c36d1c865df68e5abd9dab04bd0fa5e48 (patch)
treeadc4fb1c6d6c2bf116bf539d18fd3aba0a4e0312 /contrib
parent2164f9a1254980a02ef9ca99ee3bcb8c1298b219 (diff)
downloadpostgresql-09ac603c36d1c865df68e5abd9dab04bd0fa5e48.tar.gz
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 'contrib')
-rw-r--r--contrib/oid2name/oid2name.c38
-rw-r--r--contrib/pg_upgrade/util.c24
-rw-r--r--contrib/pgbench/pgbench.c6
3 files changed, 48 insertions, 20 deletions
diff --git a/contrib/oid2name/oid2name.c b/contrib/oid2name/oid2name.c
index 17d4d80fc9..a666731e72 100644
--- a/contrib/oid2name/oid2name.c
+++ b/contrib/oid2name/oid2name.c
@@ -51,6 +51,7 @@ struct options
static void help(const char *progname);
void get_opts(int, char **, struct options *);
void *pg_malloc(size_t size);
+void *pg_realloc(void *ptr, size_t size);
char *pg_strdup(const char *str);
void add_one_elt(char *eltname, eary *eary);
char *get_comma_elts(eary *eary);
@@ -203,16 +204,37 @@ help(const char *progname)
void *
pg_malloc(size_t size)
{
- void *ptr = malloc(size);
+ void *ptr;
+ /* Avoid unportable behavior of malloc(0) */
+ if (size == 0)
+ size = 1;
+ ptr = malloc(size);
if (!ptr)
{
- fprintf(stderr, "out of memory");
+ fprintf(stderr, "out of memory\n");
exit(1);
}
return ptr;
}
+void *
+pg_realloc(void *ptr, size_t size)
+{
+ void *result;
+
+ /* Avoid unportable behavior of realloc(NULL, 0) */
+ if (ptr == NULL && size == 0)
+ size = 1;
+ result = realloc(ptr, size);
+ if (!result)
+ {
+ fprintf(stderr, "out of memory\n");
+ exit(1);
+ }
+ return result;
+}
+
char *
pg_strdup(const char *str)
{
@@ -220,7 +242,7 @@ pg_strdup(const char *str)
if (!result)
{
- fprintf(stderr, "out of memory");
+ fprintf(stderr, "out of memory\n");
exit(1);
}
return result;
@@ -242,14 +264,8 @@ add_one_elt(char *eltname, eary *eary)
else if (eary->num >= eary->alloc)
{
eary ->alloc *= 2;
- eary ->array = (char **)
- realloc(eary->array, eary->alloc * sizeof(char *));
-
- if (!eary->array)
- {
- fprintf(stderr, "out of memory");
- exit(1);
- }
+ eary ->array = (char **) pg_realloc(eary->array,
+ eary->alloc * sizeof(char *));
}
eary ->array[eary->num] = pg_strdup(eltname);
diff --git a/contrib/pg_upgrade/util.c b/contrib/pg_upgrade/util.c
index d879e762fa..1d4bc89f0b 100644
--- a/contrib/pg_upgrade/util.c
+++ b/contrib/pg_upgrade/util.c
@@ -192,33 +192,39 @@ get_user_info(char **user_name)
void *
-pg_malloc(size_t n)
+pg_malloc(size_t size)
{
- void *p = malloc(n);
+ void *p;
+ /* Avoid unportable behavior of malloc(0) */
+ if (size == 0)
+ size = 1;
+ p = malloc(size);
if (p == NULL)
pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
-
return p;
}
void *
-pg_realloc(void *ptr, size_t n)
+pg_realloc(void *ptr, size_t size)
{
- void *p = realloc(ptr, n);
+ void *p;
+ /* Avoid unportable behavior of realloc(NULL, 0) */
+ if (ptr == NULL && size == 0)
+ size = 1;
+ p = realloc(ptr, size);
if (p == NULL)
pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
-
return p;
}
void
-pg_free(void *p)
+pg_free(void *ptr)
{
- if (p != NULL)
- free(p);
+ if (ptr != NULL)
+ free(ptr);
}
diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c
index 509472262b..a669cf406b 100644
--- a/contrib/pgbench/pgbench.c
+++ b/contrib/pgbench/pgbench.c
@@ -299,6 +299,9 @@ pg_malloc(size_t size)
{
void *result;
+ /* Avoid unportable behavior of malloc(0) */
+ if (size == 0)
+ size = 1;
result = malloc(size);
if (!result)
{
@@ -313,6 +316,9 @@ pg_realloc(void *ptr, size_t size)
{
void *result;
+ /* Avoid unportable behavior of realloc(NULL, 0) */
+ if (ptr == NULL && size == 0)
+ size = 1;
result = realloc(ptr, size);
if (!result)
{