https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104069
--- Comment #18 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #14)
> Removing the test for !size from the first conditional avoids the warning.
> I don't fully understand what the code tries to do but the following avoids
> it at -O2 (only):
>
> void *xrealloc (void *ptr, int size)
> {
> if (!size)
> size = 1;
> void *ret = __builtin_realloc (ptr, size);
> if (!ret)
> ret = __builtin_realloc (ptr, 1);
> if (!ret) {
> ret = __builtin_realloc (ptr, size);
> if (!ret)
> ret = __builtin_realloc(ptr, 1);
> if (!ret)
> __builtin_abort ();
> }
> return ret;
> }
This is definitely not what the code meant to do.
If I do xrealloc (malloc (30), 1024 * 1024 * 1024) and it returns non-NULL,
it certainly shouldn't be that it under the hood called realloc (ptr, 1)
because
the first realloc failed.
If you add that if (!size) size = 1;, then I think what is meant is just
void *ret = __builtin_realloc (ptr, size);
if (!ret)
{
ret = __builtin_realloc (ptr, size);
if (!ret)
die (...);
}
return ret;