BUG: realloc(p,0) should be consistent with malloc(0)

Florian Weimer fweimer@redhat.com
Thu Jun 19 09:20:51 GMT 2025


* Alejandro Colomar:

> And yes, it means that realloc(p,0) can result in returning NULL without
> setting errno, and thus passing the input pointer to free(3).  However,
> the only situation in which this can happen is if p is a null pointer
> itself.  This is explicitly said in the RETURN VALUE section, which is
> normative.
>
> RETURN VALUE
>
> 	If size is 0,
> 	...
> 	either:
>
> 	    -  A null pointer shall be returned and,
> 	       if ptr is not a null pointer, errno shall be set to EINVAL.
>
> 	    -  A pointer to the allocated space shall be returned, ...
>
> You're acting as if you didn't read that normative section, which is
> very explicit in what can happen.

The part you elided, it also says this:

| Upon successful completion, realloc() and reallocarray() shall return
| a pointer to the new object (which can have the same value as a
| pointer to the old object), or a null pointer if the new object has
| not been allocated.

I don't see anything clearing stating that the “has not been allocated”
case is restricted to error scenarios only.

Thanks,
Florian



More information about the Libc-alpha mailing list