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

Joakim Tjernlund joakim.tjernlund at transmode.se
Thu Oct 21 13:25:52 CEST 2010


> 
> In case malloc is invoked with requested size 0, this patch will prevent
> the execution of the allocation algorithm (because it corrupts the data 
> structures)
> and will return 0 to the caller.
> 
> Signed-off-by: Nikolaos Kostaras <nkost at intracomdefense.com>
> 
> ---
>  common/dlmalloc.c |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
> 
> diff --git a/common/dlmalloc.c b/common/dlmalloc.c
> index fce7a76..d9e3ea9 100644
> --- a/common/dlmalloc.c
> +++ b/common/dlmalloc.c
> @@ -2182,7 +2182,7 @@ Void_t* mALLOc(bytes) size_t bytes;
>      return 0;
>    }
> 
> -  if ((long)bytes < 0) return 0;
> +  if ((long)bytes <= 0) return 0;

I think you should return some impossible ptr value =! NULL
Size 0 not really an error. 
In free you do:
if (impossible ptr)
   return;

If you can't find a good ptr value you could just do:
if (!bytes)
   bytes = 1;


More information about the U-Boot mailing list