Browse Source

POSIX requires that errno be set whenever 0 is returned by malloc()

Mike Frysinger 17 years ago
parent
commit
c9210d3814

+ 2 - 1
libc/stdlib/malloc-simple/alloc.c

@@ -32,7 +32,8 @@ void *malloc(size_t size)
 		size++;
 #else
 		/* Some programs will call malloc (0).  Lets be strict and return NULL */
-		return 0;
+		__set_errno(ENOMEM);
+		return NULL;
 #endif
 	}
 

+ 4 - 1
libc/stdlib/malloc-standard/malloc.c

@@ -826,7 +826,10 @@ void* malloc(size_t bytes)
     void *          retval;
 
 #if !defined(__MALLOC_GLIBC_COMPAT__)
-    if (!bytes) return NULL;
+    if (!bytes) {
+        __set_errno(ENOMEM);
+        return NULL;
+    }
 #endif
 
     __MALLOC_LOCK;

+ 1 - 1
libc/stdlib/malloc/malloc.c

@@ -200,7 +200,7 @@ malloc (size_t size)
 #else
   /* Some programs will call malloc (0).  Lets be strict and return NULL */
   if (unlikely (size == 0))
-    return 0;
+    goto oom;
 #endif
 
   /* Check if they are doing something dumb like malloc(-1) */