[U-Boot] [PATCH] Prevent malloc with size 0

Joakim Tjernlund joakim.tjernlund at transmode.se
Thu Oct 21 13:56:49 CEST 2010


Wolfgang Denk <wd at denx.de> wrote on 2010/10/21 13:51:26:
> 
> Dear Joakim Tjernlund,
> 
> In message <OFD5ABFC5E.96E88C93-ONC12577C3.00406E0E-C12577C3.
> 00408F11 at transmode.se> you wrote:
> >
> > > It is legal for malloc() to return NULL in case of size==0,
> > > and for the sake of simplicity I recommend we do just that.
> > 
> > Yes, but not very useful. Glibc does not return NULL
> 
> Maybe not in the current implementation, and not on the architecture
> you checked. Current doc reads: "If size is 0, then malloc() returns
> either NULL, or a unique pointer value that can later be successfully
> passed to free()."

On linux and the tests I made, the unique pointer value is what I get.
Possibly it is configurable.

> 
> Of course we could return some valid pointer like glibc does, i. e.
> implement something like
> 
>    if (size == 0)
>       size = 8;
> 
> or so.  Do you think that would be better?

Better than NULL, but best would be a ptr that will SEGV if
you try to defer it. Not the easiest to impl., perhaps
~0 will do?

     Jocke


More information about the U-Boot mailing list