]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
malloc: Fix UB in malloc-debug
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Fri, 18 Apr 2025 13:08:21 +0000 (10:08 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 7 May 2025 17:21:21 +0000 (14:21 -0300)
Multiple tests fail when malloc-debug is built with ubsan:

UBSAN: Undefined behaviour in malloc-debug.c:231:24 applying non-zero offset to a NULL pointer

The main issue is it tries to apply DUMPED_MAIN_ARENA_CHUNK or
for mem2chunk for NULL pointers.

malloc/malloc-debug.c

index d208aa32a3c650cbce88f7bc04cc85969db56dcf..2edefad2f0d2bf3bc278fe1efc0a69a04e6ec084 100644 (file)
@@ -202,7 +202,7 @@ __debug_free (void *mem)
   if (__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK))
     mem = free_mcheck (mem);
 
-  if (DUMPED_MAIN_ARENA_CHUNK (mem2chunk (mem)))
+  if (mem != NULL && DUMPED_MAIN_ARENA_CHUNK (mem2chunk (mem)))
     /* Do nothing.  */;
   else if (__is_malloc_debug_enabled (MALLOC_CHECK_HOOK))
     free_check (mem);
@@ -227,7 +227,7 @@ __debug_realloc (void *oldmem, size_t bytes)
   if ((!__is_malloc_debug_enabled (MALLOC_MCHECK_HOOK)
        || !realloc_mcheck_before (&oldmem, &bytes, &oldsize, &victim)))
     {
-      mchunkptr oldp = mem2chunk (oldmem);
+      mchunkptr oldp = oldmem != NULL ? mem2chunk (oldmem) : NULL;
 
       /* If this is a faked mmapped chunk from the dumped main arena,
         always make a copy (and do not free the old chunk).  */