pg_malloc() versus malloc(0)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org
Subject: pg_malloc() versus malloc(0)
Date: 2012-10-01 14:00:46
Message-ID: 23126.1349100046@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Per
http://archives.postgresql.org/pgsql-general/2012-10/msg00013.php
we have got a problem with the pg_malloc() interface functions that
were recently added to pg_dump and a lot of other frontend code.
Namely, that on platforms where malloc(0) returns NULL instead of
a pointer to a zero-size block, pg_malloc thinks it's a failure
and aborts the program.

There are basically two ways we could fix this:

1. Teach pg_malloc not to complain if result == NULL and size == 0.

2. Before the malloc call, have it replace size == 0 with size = 1.

#2 would guarantee no NULL returns from pg_malloc, which would be closer
to the behavior of palloc in the backend. On the other hand, it seems
a bit wasteful and inelegant. Any code that was capable of calling
malloc(0) before is presumably not going to be upset by a NULL return,
or we'd have seen trouble reports sooner.

Any opinions which way to go? I'm not convinced either way yet.

regards, tom lane

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Karl O. Pinc 2012-10-01 14:05:32 Re: Doc patch, further describe and-mask nature of the permission system v2
Previous Message Misa Simic 2012-10-01 13:52:21 Re: is JSON really "a type" (Re: data to json enhancements)