Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 12 Dec 2001 15:42:54 -0800
From:      Peter Wemm <peter@wemm.org>
To:        Poul-Henning Kamp <phk@FreeBSD.org>
Cc:        cvs-committers@FreeBSD.org, cvs-all@FreeBSD.org
Subject:   Re: cvs commit: src/lib/libc/stdlib malloc.c 
Message-ID:  <20011212234254.2BEA53810@overcee.netplex.com.au>
In-Reply-To: <200111021132.fA2BWSs53697@freefall.freebsd.org> 

next in thread | previous in thread | raw e-mail | index | archive | help
Poul-Henning Kamp wrote:
> phk         2001/11/02 03:32:28 PST
> 
>   Modified files:
>     lib/libc/stdlib      malloc.c 
>   Log:
>   phkmalloc->evilchecks++;
>   
>   If zero bytes are allocated, return pointer to the middle of page-zero
>   (which is protected) so that the program will crash if it dereferences
>   this illgotten pointer.

Since this broke binutils I spoke with somebody from that side of the
fence.. Here's the quote:

"Incidentally, this is not standards compliant:

  ISO/IEC 9899:1999 7.20.3#1:
  If the size of the space requested is zero, the behavior is implementation-
  defined: either a null pointer is returned, or the behavior is as if the
  size were some nonzero value, except that the returned pointer shall not
  be used to access an object.

Note "implementation-defined" not "undefined".  So

  char *a, *b;
  a = malloc(0);
  b = malloc(0);
  if (a && b && a == b)
    abort ();

shall not abort.

Now, you _can_ make the returned pointer point to a PROT_NONE page,
but you still have to preserve identity of individual allocations.
"

By my reading he is right, we have two choices.. either return null or
pretend it was a malloc(1).

The bug in binutils that tripped this up was that they were doing
object->ptr = malloc(size);	// where size == 0
....
if (object->ptr)
	free(object->ptr);

Yes it was ``broken'' as such but is compatable with a compliant malloc().

They've changed bfd_alloc(size) (the wrapper around all malloc() calls)
to do this:
  if (size == 0)
    size = 1;
.. which bypasses all these checks and any implementation defined behavior.

Cheers,
-Peter
--
Peter Wemm - peter@FreeBSD.org; peter@yahoo-inc.com; peter@netplex.com.au
"All of this is for nothing if we don't go to the stars" - JMS/B5


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe cvs-all" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20011212234254.2BEA53810>