[U-Boot] [PATCH] malloc: return NULL if not initialized yet

Wolfgang Denk wd at denx.de
Fri Jan 15 11:25:17 CET 2010


When malloc() was called before it was properly initialized
(as would happen if when used before relocation to RAM) it returned
random, non-NULL values, which called all kinds of difficult to debug
subsequent errors.

Make sure to return NULL when initialization was not done yet.

Signed-off-by: Wolfgang Denk <wd at denx.de>
---
 common/dlmalloc.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index 735b344..e7a998f 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -2179,6 +2179,12 @@ Void_t* mALLOc(bytes) size_t bytes;
 
   INTERNAL_SIZE_T nb;
 
+  /* check if mem_malloc_init() was run */
+  if ((mem_malloc_start == 0) && (mem_malloc_end ==0)) {
+    /* not initialized yet */
+    return 0;
+  }
+
   if ((long)bytes < 0) return 0;
 
   nb = request2size(bytes);  /* padded request size; */
-- 
1.6.2.5



More information about the U-Boot mailing list