qemu-devel
[Top][All Lists]
Advanced

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

[PATCH] Make qemu_alloc()/qemu_realloc() return NULL for size==0 (was Re


From: Eduardo Habkost
Subject: [PATCH] Make qemu_alloc()/qemu_realloc() return NULL for size==0 (was Re: [Qemu-devel] [PATCH] fix qemu_malloc() error check for size==0)
Date: Tue, 19 May 2009 13:44:40 -0300
User-agent: Mutt/1.5.18 (2008-05-17)

On Tue, May 19, 2009 at 06:55:11PM +0400, malc wrote:
<snip>
> > > 
> > > That's the problem standard C does _not_ define the behaviour, and leaves
> > > that to implementation.
> > 
> > The only thing it doesn't define is either the returned pointer is NULL
> > or not, and that doesn't make malloc(0) automatically unportable,
> > because all the rest is perfectly defined:
> > 
> > 1) You can't dereference the pointer (just like you can't
> >    dereference p[n] on a malloc(n) block)
> > 2) You should pass the returned pointer to free() later
> > 
> 
> Alas your list is not exhaustive:
> 
>   3) Test the returned value against NULL
> 
> [Which is precisely what the qcow2 code did]
> 
[...]
> > 
> > I agree that expecting the Linux behaviour (non-NULL) is a bug. My point
> > is that there is no reason to consider malloc(0) a bug.
> 
> There is, due to the possibility of performing a 3) and a hard time 
> catching that (unless someone solves halting problem or subset applicable
> to QEMU thereof)

This is probably the only of your points which I agree with. What about
the following, then?

That would catch the cases you are worried about, but won't break
existing cases where malloc(0) is used correctly, and we won't be
creating a new malloc/free API that is incompabible from every other
malloc/free API out there.

-----------
>From c2e0dd58e3eb7e2fd749bcf4999f067b1ba19083 Mon Sep 17 00:00:00 2001
From: Eduardo Habkost <address@hidden>
Date: Tue, 19 May 2009 13:24:47 -0300
Subject: [PATCH] Make qemu_alloc()/qemu_realloc() return NULL for size==0

This is an alternative to the fix I've proposed at:

Message-Id: <address@hidden>
http://marc.info/?l=qemu-devel&m=124267871605176

Instead of returning whatever realloc(p, 0) or malloc(0) of the system we are
running on returns, always return NULL on those cases.

This should make it easier to catch cases where code is incorrectly checking
the qemu_malloc() or qemu_realloc() return value for NULL when when size==0.

Signed-off-by: Eduardo Habkost <address@hidden>
---
 qemu-malloc.c |   13 +++++++++----
 1 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/qemu-malloc.c b/qemu-malloc.c
index 6761857..5346ece 100644
--- a/qemu-malloc.c
+++ b/qemu-malloc.c
@@ -43,15 +43,20 @@ void qemu_free(void *ptr)
 
 void *qemu_malloc(size_t size)
 {
+    if (!size)
+        return NULL;
+
     return oom_check(malloc(size));
 }
 
 void *qemu_realloc(void *ptr, size_t size)
 {
-    if (size)
-        return oom_check(realloc(ptr, size));
-    else
-        return realloc(ptr, size);
+    if (!size) {
+        free(ptr);
+        return NULL;
+    }
+
+    return oom_check(realloc(ptr, size));
 }
 
 void *qemu_mallocz(size_t size)
-- 
1.6.3.rc4.29.g8146


-- 
Eduardo




reply via email to

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