Why pfree(NULL) breaks execution?

Started by Marios Vodasalmost 15 years ago4 messages
#1Marios Vodas
mvodas@gmail.com

C doesn't break on free(NULL) so why is pfree developed to break on NULL?
Is there any way in PostgreSQL to overcome this so that it won't break,
apart from checking if the pointer NULL?

#2Robert Haas
robertmhaas@gmail.com
In reply to: Marios Vodas (#1)
Re: Why pfree(NULL) breaks execution?

On Fri, Mar 4, 2011 at 8:08 AM, Marios Vodas <mvodas@gmail.com> wrote:

C doesn't break on free(NULL) so why is pfree developed to break on NULL?
Is there any way in PostgreSQL to overcome this so that it won't break,
apart from checking if the pointer NULL?

I think that free(NULL) works on some platforms but not all. I don't
see what advantage we'd get out of making pfree(NULL) silently work,
and there's a clear disadvantage: it would remove a useful sanity
check.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

#3Peter Geoghegan
peter.geoghegan86@gmail.com
In reply to: Robert Haas (#2)
Re: Why pfree(NULL) breaks execution?

On 4 March 2011 14:50, Robert Haas <robertmhaas@gmail.com> wrote:

I think that free(NULL) works on some platforms but not all.  I don't
see what advantage we'd get out of making pfree(NULL) silently work,
and there's a clear disadvantage: it would remove a useful sanity
check.

I don't feel particularly strongly about what pfree() should do one
way or the other, but that isn't so; free(NULL) works on all
platforms, and is required to by the standard.

--
Regards,
Peter Geoghegan

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Geoghegan (#3)
Re: Why pfree(NULL) breaks execution?

Peter Geoghegan <peter.geoghegan86@gmail.com> writes:

On 4 March 2011 14:50, Robert Haas <robertmhaas@gmail.com> wrote:

I think that free(NULL) works on some platforms but not all. �I don't
see what advantage we'd get out of making pfree(NULL) silently work,
and there's a clear disadvantage: it would remove a useful sanity
check.

I don't feel particularly strongly about what pfree() should do one
way or the other, but that isn't so; free(NULL) works on all
platforms, and is required to by the standard.

For the last few years it's been pretty safe to assume that, but it did
not use to be so --- pre ISO C spec, some malloc libraries allowed
free(NULL) and some didn't.

In any case, this has been debated before and the project policy is
that having pfree(NULL) throw an error is a net benefit. The main case
where it's really useful to not throw an error is where malloc(0)
returns NULL rather than a valid pointer (and BTW, both of those
behaviors are allowed by spec). However, palloc(0) is guaranteed
to give you a valid pointer that you can pfree, so that argument
doesn't hold here.

regards, tom lane