BUG: realloc(p,0) should be consistent with malloc(0)
Wilco Dijkstra
Wilco.Dijkstra@arm.com
Tue Jun 17 19:10:56 GMT 2025
Hi Alejandro,
>> "If size is non-zero and memory for the new object is not allocated, the old object shall not be deallocated."
>
> Read the first 4 words of that sentence. "If size is non-zero". We're
> talking about realloc(p,0). That sentence you've quoted doesn't cover
> this scenario.
And those are the 4 words that allow one to call free(p) AND return NULL.
Without those 4 words it would demand for zero size that p "shall not be deallocated"
and thus make GLIBCs current behaviour non-conforming.
> Current POSIX.1-2024 is fine. glibc is not.
How is it fine if it makes GLIBCs implementation legal? It would have to be fixed too.
The only difference between POSIX and ISO C is that POSIX defines a way to differentiate
between these cases by checking errno. But that's not really useful since not all allocators
set errno, and nobody resets and checks errno after each realloc.
Cheers,
Wilco
More information about the Libc-alpha
mailing list