[uClibc-cvs] uClibc/libc/stdlib/malloc malloc.c, 1.34, 1.35 realloc.c, 1.18, 1.19

Erik Andersen andersen at uclibc.org
Sat Sep 6 11:49:30 UTC 2003


Update of /var/cvs/uClibc/libc/stdlib/malloc
In directory winder:/tmp/cvs-serv26343/malloc

Modified Files:
	malloc.c realloc.c 
Log Message:
Fix errno values.  Fix MALLOC_GLIBC_COMPAT handling in malloc/malloc.c, 
which was reversed.  Provide more consistancy between implementations.
Handle it when people do stupid things like malloc(-1);


Index: malloc.c
===================================================================
RCS file: /var/cvs/uClibc/libc/stdlib/malloc/malloc.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -d -r1.34 -r1.35
--- malloc.c	8 Aug 2003 10:30:11 -0000	1.34
+++ malloc.c	6 Sep 2003 11:49:27 -0000	1.35
@@ -13,6 +13,7 @@
 
 #include <stdlib.h>
 #include <unistd.h>
+#include <errno.h>
 #include <sys/mman.h>
 
 #include "malloc.h"
@@ -173,6 +174,7 @@
 void *
 malloc (size_t size)
 {
+  void *mem;
 #ifdef MALLOC_DEBUGGING
   static int debugging_initialized = 0;
   if (! debugging_initialized)
@@ -185,12 +187,22 @@
 #endif
 
 #if defined(__MALLOC_GLIBC_COMPAT__)
-  if (size == 0)
-    return 0;
-#else
-  if (size == 0)
+  if (unlikely(size == 0))
       size++;
+#else
+  /* Some programs will call malloc (0).  Lets be strict and return NULL */
+  if (unlikely(size == 0))
+      goto oom;
 #endif
+  /* Check if they are doing something dumb like malloc(-1) */
+  if (unlikely(((unsigned long)size > (unsigned long)(MALLOC_HEADER_SIZE*-2))))
+      goto oom;
 
-  return malloc_from_heap (size, &__malloc_heap);
+  mem = malloc_from_heap (size, &__malloc_heap);
+  if (unlikely(!mem)) {
+oom:
+      __set_errno(ENOMEM);
+      return NULL;
+  }
+  return mem;
 }

Index: realloc.c
===================================================================
RCS file: /var/cvs/uClibc/libc/stdlib/malloc/realloc.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- realloc.c	21 Nov 2002 06:06:21 -0000	1.18
+++ realloc.c	6 Sep 2003 11:49:27 -0000	1.19
@@ -13,6 +13,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 
 #include "malloc.h"
 #include "heap.h"
@@ -25,13 +26,12 @@
   char *base_mem;
 
   /* Check for special cases.  */
-  if (! new_size)
-    {
+  if (!mem)
+      return malloc(new_size);
+  if (!new_size) {
       free (mem);
-      return 0;
-    }
-  else if (! mem)
-    return malloc (new_size);
+      return (malloc(new_size));
+  }
 
   /* Normal realloc.  */
 




More information about the uClibc-cvs mailing list