Skip to contentSkip to conversation

malloc: return NULL if not initialized yet

PATCH New Wolfgang Denk 2 participants 4 messages Jan 15, 2010 – Jan 17, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 malloc: return NULL if not initialized yet

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.

---
 common/dlmalloc.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
common/dlmalloc.c +6 -0
index 735b344..e7a998f 100644
@@ -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; */
Signature · 1 line
1.6.2.5
#1 lore raw

PATCH 1/1 malloc: return NULL if not initialized yet

Quoted 1 line · On Friday 15 January 2010 05:25:17 Wolfgang Denk wrote:

+ if ((mem_malloc_start == 0) && (mem_malloc_end ==0)) {

missing space ? "==0" -> "== 0" ?
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20100115/85e9baf9/attachment.pgp

#2 lore raw

PATCH 1/1 malloc: return NULL if not initialized yet

Dear Mike Frysinger,

Quoted 5 lines · In message <[email protected]> you wrote:
Quoted 1 line · On Friday 15 January 2010 05:25:17 Wolfgang Denk wrote:

+ if ((mem_malloc_start == 0) && (mem_malloc_end ==0)) {

missing space ? "==0" -> "== 0" ?

Right. Will fix this then applying.

Thanks for catching.

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Another war ... must it always be so?  How many comrades have we lost
in this way? ...  Obedience.  Duty.  Death, and more death ...
	-- Romulan Commander, "Balance of Terror", stardate 1709.2
#3 lore raw

PATCH 1/1 malloc: return NULL if not initialized yet

In reply to Wolfgang Denk
Quoted 11 lines · In message <[email protected]> you wrote:

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.

---
 common/dlmalloc.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

Applied (with change as suggested by Mike).

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Disobedience:  The silver lining to the cloud of servitude.
- Ambrose Bierce