]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Fix allocation_index increment in malloc_internal
authorOsama Abdelkader <osama.abdelkader@gmail.com>
Mon, 1 Dec 2025 12:35:36 +0000 (13:35 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Mon, 1 Dec 2025 12:35:36 +0000 (13:35 +0100)
The allocation_index was being incremented before checking if mmap()
succeeds.  If mmap() fails, allocation_index would still be incremented,
creating a gap in the allocations tracking array and making
allocation_index inconsistent with the actual number of successful
allocations.

This fix moves the allocation_index increment to after the mmap()
success check, ensuring it only increments when an allocation actually
succeeds.  This maintains proper tracking for leak detection and
prevents gaps in the allocations array.

Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
Reviewed-by: Florian Weimer <fweimer@redhat.com>
malloc/tst-interpose-aux.c

index cf4b8ab25e84ce31e664df28e5f9b43cdb9b8913..55e9623ae0b84e3f44ad98ab5c5b55d2a5feb819 100644 (file)
@@ -157,11 +157,11 @@ malloc_internal (size_t size)
       return NULL;
     }
 
-  size_t index = allocation_index++;
   void *result = mmap (NULL, allocation_size, PROT_READ | PROT_WRITE,
                        MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   if (result == MAP_FAILED)
     return NULL;
+  size_t index = allocation_index++;
   allocations[index] = result;
   *allocations[index] = (struct allocation_header)
     {