realloc in buf_append

Nikos Mavrogiannopoulos n.mavrogiannopoulos at gmail.com
Mon Feb 18 04:30:58 EST 2013


On Mon, Feb 18, 2013 at 1:06 AM, David Woodhouse <dwmw2 at infradead.org> wrote:
>> int safe_realloc(void **ptr, size_t size)
>> {
>>         void *newptr = realloc(*ptr, size);
>>         if (newptr) {
>>                 *ptr = newptr;
>>                 return 0;
>>         }
>>         free(*ptr);
>>         return -ENOMEM;
>> }
>
> I think I prefer a 'realloc_or_free' function which just *does* what
> what all the existing code already expects: just free the original
> pointer if it fails to allocate the new one.
[...]
> GnuTLS has a vaguely similar 'gnutls_realloc_fast()' function which it
> doesn't export, and which doesn't free the original pointer if called
> with a size argument of zero. Although I suspect the latter is a bug.

I never used realloc to free data so I never bothered adding it on
that internal function. However, I remember I was very surprised to
find out that the C realloc() had such behavior, i.e., a memory leak
if used as prescribed.

Nevertheless, on a typical Linux system you'll never see a NULL coming
from memory allocation functions. Most likely you'll receive a signal
by the oom killer.

regards,
Nikos



More information about the openconnect-devel mailing list