On Thu, Jul 31, 2014 at 18:10, Remco wrote:
> Fritjof Bornebusch wrote:
> 
>> Hi tech,
>>
>> there is an unnecessary NULL check before calling free.
>>
>> fritjof
>>
>> Index: xmalloc.c
>> ===================================================================
>> RCS file: /cvs/src/usr.bin/rcs/xmalloc.c,v
>> retrieving revision 1.4
>> diff -u -p -r1.4 xmalloc.c
>> --- xmalloc.c   7 Jun 2009 08:39:13 -0000       1.4
>> +++ xmalloc.c   31 May 2014 19:19:18 -0000
>> @@ -76,8 +76,6 @@ xrealloc(void *ptr, size_t nmemb, size_t
>>  void
>>  xfree(void *ptr)
>>  {
>> -       if (ptr == NULL)
>> -               errx(1, "xfree: NULL pointer given as argument");
>>         free(ptr);
>>  }
> 
> Looking for xmalloc/xfree on the internet it seems this behaviour is
> intended. On OpenBSD at least malloc(3) has a reference to xmalloc by means
> of its 'X' malloc option.

Half true. :)

The behavior is intended. I don't really know why they care about
freeing null, but the intention is clearly to check for it; otherwise
they would just call free() in the first place. (actually, i think the
rationale is that to assure portability, you need a strict version of
free that matches the broken version found elsewhere or you will
accidentally do something not portable. under that assumption, and
assuming we don't care about those platforms, the correct fix is to
delete xfree entirely.)

The X option, though, to malloc.conf doesn't check NULL to free. It
just makes malloc abort instead of returning null. I don't think it's
a useful option either way. Don't use it.

Reply via email to