[PATCH] Fix allocation_index increment in malloc_internal
Florian Weimer
fweimer@redhat.com
Mon Dec 1 12:34:46 GMT 2025
* Osama Abdelkader:
> 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>
> ---
> malloc/tst-interpose-aux.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/malloc/tst-interpose-aux.c b/malloc/tst-interpose-aux.c
> index cf4b8ab25e..55e9623ae0 100644
> --- a/malloc/tst-interpose-aux.c
> +++ b/malloc/tst-interpose-aux.c
> @@ -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)
> {
This is okay. I'm going to push it for you.
Reviewed-by: Florian Weimer <fweimer@redhat.com>
Thanks,
Florian
More information about the Libc-alpha
mailing list