Skip to content

vutils/system_allocator: Always return NULL on zero-size realloc#145

Merged
Grillo-0 merged 1 commit into
developfrom
0-realloc-return-null
Jun 13, 2026
Merged

vutils/system_allocator: Always return NULL on zero-size realloc#145
Grillo-0 merged 1 commit into
developfrom
0-realloc-return-null

Conversation

@Grillo-0

Copy link
Copy Markdown
Member

sys_malloc and sys_calloc already short-circuit to NULL when bytes * times == 0, but sys_realloc was forwarding straight to libc realloc(ptr, 0), whose behavior is implementation-defined (and undefined as of C23). On macOS that returns a valid 1-byte allocation, so the existing zero-size realloc test asserted NULL and failed.

Mirror the malloc/calloc guard in sys_realloc, also freeing the old buffer to avoid leaking it when the caller shrinks to zero. The arena backend already returns NULL in this case, so vut_allocator_realloc is now consistent across both backends and all platforms.

`sys_malloc` and `sys_calloc` already short-circuit to NULL when
`bytes * times == 0`, but `sys_realloc` was forwarding straight to libc
`realloc(ptr, 0)`, whose behavior is implementation-defined (and
undefined as of C23). On macOS that returns a valid 1-byte allocation,
so the existing zero-size realloc test asserted NULL and failed.

Mirror the malloc/calloc guard in `sys_realloc`, also freeing the old
buffer to avoid leaking it when the caller shrinks to zero. The arena
backend already returns NULL in this case, so vut_allocator_realloc is
now consistent across both backends and all platforms.
@Grillo-0 Grillo-0 self-assigned this Jun 12, 2026
@Grillo-0 Grillo-0 merged commit 7add822 into develop Jun 13, 2026
3 checks passed
@Grillo-0 Grillo-0 deleted the 0-realloc-return-null branch June 13, 2026 18:46
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.

3 participants