Fix allocation_index increment in malloc_internal

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>
This commit is contained in:
Osama Abdelkader 2025-12-01 13:35:36 +01:00 committed by Florian Weimer
parent f9e61cd446
commit 57ce2d8243
1 changed files with 1 additions and 1 deletions

View File

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