Skip to content

realloc(NULL, 0) is malloc(0), fix realloc of small big-aligned chunks - #247

Merged
struct merged 1 commit into
masterfrom
realloc_zero_size_nov_2026
Dec 5, 2026
Merged

struct merged 1 commit into
masterfrom
realloc_zero_size_nov_2026

Conversation

@struct

@struct struct commented Dec 5, 2026

Copy link
Copy Markdown
Owner

realloc(NULL, 0) returned NULL, which gnulib's xrealloc treats as
out of memory, so grep and sed died with "memory exhausted" under
LD_PRELOAD. realloc(NULL, size) now calls iso_alloc(size) for every
size, so it returns exactly what malloc(0) returns: the shared
no-access page by default, or a unique chunk when built with
NO_ZERO_ALLOCATIONS=0.

A request with an alignment above the page size is served from a
big zone even when the size is small. realloc of such a chunk
released it through _iso_free_size, which only searched the small
zones and aborted with "Could not find zone". It now falls back to
the big zone list.

Adds tests/preload/libc_semantics.c covering zero sized malloc,
calloc, realloc and reallocarray, realloc(NULL, 0) matching
malloc(0), and posix_memalign / aligned_alloc for every power of
two up to 4 MB followed by realloc. On Linux the preload runner
also runs grep, sed, sort and ls under the library.

Reported in daanx/mimalloc-bench#295

Co-Authored-By: Claude Fable 5.2 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01NBUcR6B9g4yn7x7fstjXfk

realloc(NULL, 0) returned NULL, which gnulib's xrealloc treats as
out of memory, so grep and sed died with "memory exhausted" under
LD_PRELOAD. realloc(NULL, size) now calls iso_alloc(size) for every
size, so it returns exactly what malloc(0) returns: the shared
no-access page by default, or a unique chunk when built with
NO_ZERO_ALLOCATIONS=0.

A request with an alignment above the page size is served from a
big zone even when the size is small. realloc of such a chunk
released it through _iso_free_size, which only searched the small
zones and aborted with "Could not find zone". It now falls back to
the big zone list.

Adds tests/preload/libc_semantics.c covering zero sized malloc,
calloc, realloc and reallocarray, realloc(NULL, 0) matching
malloc(0), and posix_memalign / aligned_alloc for every power of
two up to 4 MB followed by realloc. On Linux the preload runner
also runs grep, sed, sort and ls under the library.

Reported in daanx/mimalloc-bench#295

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NBUcR6B9g4yn7x7fstjXfk
@struct
struct merged commit 835af38 into master Dec 5, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant