Skip to contentSkip to conversation

fix qemu_malloc(0)

PATCH In review Gerd Hoffmann 2 participants 2 messages May 29, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix qemu_malloc(0)

Don't abort. Return malloc(1) instead as suggested by Anthony.

Likewise kill the abort call in qemu_realloc(), we can just call free(ptr) for the size == 0 case to match realloc() behavior.

---
 qemu-malloc.c |    9 +++------
 1 files changed, 3 insertions(+), 6 deletions(-)
qemu-malloc.c +3 -6
index 295d185..4193fdb 100644
@@ -45,7 +45,7 @@ void qemu_free(void *ptr)
 void *qemu_malloc(size_t size)
 {
     if (!size) {
-        abort();
+        size = 1;
     }
     return oom_check(malloc(size));
 }
@@ -54,12 +54,9 @@ void *qemu_realloc(void *ptr, size_t size)
 {
     if (size) {
         return oom_check(realloc(ptr, size));
-    } else {
-        if (ptr) {
-            return realloc(ptr, size);
-        }
     }
-    abort();
+    free(ptr);
+    return NULL;
 }
 
 void *qemu_mallocz(size_t size)
Signature · 1 line
1.6.2.2
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Gerd Hoffmann

Don't abort. Return malloc(1) instead as suggested by Anthony.

Likewise kill the abort call in qemu_realloc(), we can just call free(ptr) for the size == 0 case to match realloc() behavior.

But we're going to have to get some more input from other maintainers before making a final decision.

Regards,

Anthony Liguori

Quoted 33 lines
---
 qemu-malloc.c |    9 +++------
 1 files changed, 3 insertions(+), 6 deletions(-)
qemu-malloc.c +3 -6
index 295d185..4193fdb 100644
@@ -45,7 +45,7 @@ void qemu_free(void *ptr)
 void *qemu_malloc(size_t size)
 {
     if (!size) {
-        abort();
+        size = 1;
     }
     return oom_check(malloc(size));
 }
@@ -54,12 +54,9 @@ void *qemu_realloc(void *ptr, size_t size)
 {
     if (size) {
         return oom_check(realloc(ptr, size));
-    } else {
-        if (ptr) {
-            return realloc(ptr, size);
-        }
     }
-    abort();
+    free(ptr);
+    return NULL;
 }
 
 void *qemu_mallocz(size_t size)