qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH] fix qemu_malloc() error check for size==0


From: Eduardo Habkost
Subject: Re: [Qemu-devel] [PATCH] fix qemu_malloc() error check for size==0
Date: Mon, 18 May 2009 19:17:05 -0300
User-agent: Mutt/1.5.18 (2008-05-17)

On Tue, May 19, 2009 at 01:56:55AM +0400, malc wrote:
> On Mon, 18 May 2009, Eduardo Habkost wrote:
> 
> > This patch is similar to a previous qemu_realloc() fix
> > (commit 322691a5c9f1c8531554148d47c078b5be590805), but for qemu_malloc().
> > 
> > malloc(0) may correctly return NULL if size==0. We don't want to abort qemu 
> > on
> > this case.
> 
> Only it wouldn't (on Linux):
> 
> $ cat malloc.c
> #include <stdlib.h>
> 
> int main (void)
> {
>     printf ("%p\n", malloc (0));
>     return 0;
> }
> $ gcc malloc.c
> $ ./a.out 
> 0x10011008
> 
> Standard (in 7.20.3) says that malloc's behaviour in case of size being
> zero is implementation defined.
> 
> Try `git show 63c75dcd669d011f438421980b4379827da4bb1c'.
> 
> The best(only?) thing to do is to check size passed to qemu_malloc[z]
> and abort the program if this situation is encountered.

Why? malloc(0) is as valid as realloc(p, 0). It will either return NULL
or a pointer, and on any case the value can be safely passed to free()
later.

-- 
Eduardo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]