diff mbox

Allow usage of qemu_realloc(ptr, 0)

Message ID 20091228154900.GE4908@volta.aurel32.net
State New
Headers show

Commit Message

Aurelien Jarno Dec. 28, 2009, 3:49 p.m. UTC
realloc(ptr, 0) is always allowed by the standard. The return value is
either NULL or a pointer that can be freed with free().

Allow usage of qemu_realloc(ptr, 0), and return NULL in that case, as
free(NULL) should always be a nop.

This fixes -kernel with stripped kernels.

Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
---
 qemu-malloc.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

Comments

Aurelien Jarno Dec. 28, 2009, 8:12 p.m. UTC | #1
On Mon, Dec 28, 2009 at 04:49:00PM +0100, Aurelien Jarno wrote:
> realloc(ptr, 0) is always allowed by the standard. The return value is
> either NULL or a pointer that can be freed with free().
> 
> Allow usage of qemu_realloc(ptr, 0), and return NULL in that case, as
> free(NULL) should always be a nop.

As malc explained in the other thread, this is not correct in C99. I am
therefore "cancelling" this patch.

> This fixes -kernel with stripped kernels.
> 
> Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
> ---
>  qemu-malloc.c |    6 +++---
>  1 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/qemu-malloc.c b/qemu-malloc.c
> index 5d9e34d..cf6a1f1 100644
> --- a/qemu-malloc.c
> +++ b/qemu-malloc.c
> @@ -63,10 +63,10 @@ void *qemu_realloc(void *ptr, size_t size)
>  {
>      if (size) {
>          return oom_check(realloc(ptr, size));
> -    } else if (allow_zero_malloc()) {
> -        return oom_check(realloc(ptr, size ? size : 1));
> +    } else if (ptr) {
> +        qemu_free(ptr);
>      }
> -    abort();
> +    return NULL;
>  }
>  
>  void *qemu_mallocz(size_t size)
> -- 
> 1.6.5.3
>
Michael S. Tsirkin Dec. 29, 2009, 3:38 p.m. UTC | #2
On Mon, Dec 28, 2009 at 04:49:00PM +0100, Aurelien Jarno wrote:
> realloc(ptr, 0) is always allowed by the standard. The return value is
> either NULL or a pointer that can be freed with free().
> 
> Allow usage of qemu_realloc(ptr, 0), and return NULL in that case, as
> free(NULL) should always be a nop.
> 
> This fixes -kernel with stripped kernels.
> 
> Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>

So this is superceded by
loader: don't call realloc(non_null, 0)
right?

> ---
>  qemu-malloc.c |    6 +++---
>  1 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/qemu-malloc.c b/qemu-malloc.c
> index 5d9e34d..cf6a1f1 100644
> --- a/qemu-malloc.c
> +++ b/qemu-malloc.c
> @@ -63,10 +63,10 @@ void *qemu_realloc(void *ptr, size_t size)
>  {
>      if (size) {
>          return oom_check(realloc(ptr, size));
> -    } else if (allow_zero_malloc()) {
> -        return oom_check(realloc(ptr, size ? size : 1));
> +    } else if (ptr) {
> +        qemu_free(ptr);
>      }
> -    abort();
> +    return NULL;
>  }
>  
>  void *qemu_mallocz(size_t size)
> -- 
> 1.6.5.3
> 
>
Michael S. Tsirkin Dec. 29, 2009, 4:06 p.m. UTC | #3
On Tue, Dec 29, 2009 at 05:38:17PM +0200, Michael S. Tsirkin wrote:
> On Mon, Dec 28, 2009 at 04:49:00PM +0100, Aurelien Jarno wrote:
> > realloc(ptr, 0) is always allowed by the standard. The return value is
> > either NULL or a pointer that can be freed with free().
> > 
> > Allow usage of qemu_realloc(ptr, 0), and return NULL in that case, as
> > free(NULL) should always be a nop.
> > 
> > This fixes -kernel with stripped kernels.
> > 
> > Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
> 
> So this is superceded by
> loader: don't call realloc(non_null, 0)
> right?

Oh, you said as much already. Sorry about the noise.

> > ---
> >  qemu-malloc.c |    6 +++---
> >  1 files changed, 3 insertions(+), 3 deletions(-)
> > 
> > diff --git a/qemu-malloc.c b/qemu-malloc.c
> > index 5d9e34d..cf6a1f1 100644
> > --- a/qemu-malloc.c
> > +++ b/qemu-malloc.c
> > @@ -63,10 +63,10 @@ void *qemu_realloc(void *ptr, size_t size)
> >  {
> >      if (size) {
> >          return oom_check(realloc(ptr, size));
> > -    } else if (allow_zero_malloc()) {
> > -        return oom_check(realloc(ptr, size ? size : 1));
> > +    } else if (ptr) {
> > +        qemu_free(ptr);
> >      }
> > -    abort();
> > +    return NULL;
> >  }
> >  
> >  void *qemu_mallocz(size_t size)
> > -- 
> > 1.6.5.3
> > 
> > 
>
diff mbox

Patch

diff --git a/qemu-malloc.c b/qemu-malloc.c
index 5d9e34d..cf6a1f1 100644
--- a/qemu-malloc.c
+++ b/qemu-malloc.c
@@ -63,10 +63,10 @@  void *qemu_realloc(void *ptr, size_t size)
 {
     if (size) {
         return oom_check(realloc(ptr, size));
-    } else if (allow_zero_malloc()) {
-        return oom_check(realloc(ptr, size ? size : 1));
+    } else if (ptr) {
+        qemu_free(ptr);
     }
-    abort();
+    return NULL;
 }
 
 void *qemu_mallocz(size_t size)