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.

Signed-off-by: Simon Glass <simon.glass@canonical.com>
(cherry picked from commit 2740544881)
This commit is contained in:
Simon Glass
2025-11-28 11:34:15 -07:00
parent b64f2045f7
commit 2d2532e8ff

View File

@@ -4582,6 +4582,11 @@ static void* tmalloc_small(mstate m, size_t nb) {
#if !ONLY_MSPACES
void* dlmalloc(size_t bytes) {
#ifdef __UBOOT__
/* Return NULL if not initialized yet */
if (!mem_malloc_start && !mem_malloc_end)
return NULL;
#endif
/*
Basic algorithm:
If a small request (< 256 bytes minus per-chunk overhead):