Skip site navigation (1) Skip section navigation (2)

Re: pg_malloc() versus malloc(0)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Geoghegan <peter(at)2ndquadrant(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: pg_malloc() versus malloc(0)
Date: 2012-10-01 15:00:35
Message-ID: 24519.1349103635@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-hackers
Peter Geoghegan <peter(at)2ndquadrant(dot)com> writes:
> On 1 October 2012 15:00, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> 1. Teach pg_malloc not to complain if result == NULL and size == 0.

> +1 to that proposal.

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

> I don't like that proposal on purely aesthetic grounds.

As Dimitri pointed out, there's really a third alternative, which is
to force a NULL result for pg_malloc(0), ie

void *
pg_malloc(size_t size)
{
	void	   *tmp;

+	if (size == 0)
+		return NULL;
+
	tmp = malloc(size);
	if (!tmp)
	{
		psql_error("out of memory\n");
		exit(EXIT_FAILURE);
	}
	return tmp;
}

A key advantage of either #2 or #3 over #1 is that they eliminate the
platform-dependent behavior, ie you can test anywhere and get the same
results.  #1 doesn't ensure that.

The fact that 9.2 managed to get out the door without anybody noticing
that pg_dump was basically broken on AIX (as well as any other platform
with this behavior) says to me that we need a fix that makes the
behavior not platform-specific.  Given that the majority of platforms
behave more like #2, maybe that's the best solution, but I could live
with #3 as well.

			regards, tom lane


In response to

pgsql-hackers by date

Next:From: Euler TaveiraDate: 2012-10-01 15:02:39
Subject: Re: Hash id in pg_stat_statements
Previous:From: Bruce MomjianDate: 2012-10-01 14:49:29
Subject: Re: CTE optimization fence on the todo list?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group