realloc in buf_append

David Woodhouse dwmw2 at infradead.org
Sun Feb 17 19:35:10 EST 2013


On Mon, 2013-02-18 at 00:06 +0000, David Woodhouse wrote:
> 
> 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.
> 
> void *safe_realloc(void *old, size_t size)
> {
>     void *p = realloc(old, size);
>     if (size && !p)
>         free(old);
>     return p;
> }

And of course we might as well enforce the fact that it assigns the
result back to the original pointer, thus:

/* I always coded as if it worked like this. Now it does. */
#define realloc_inplace(p, size) do {			\
	void *__realloc_old = p;			\
	p = realloc(p, size);				\
	if (size && !p)					\
		free(__realloc_old);			\
    } while (0)

I've pushed this as commit 8dad4f3. Thanks.

-- 
dwmw2
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 6171 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/openconnect-devel/attachments/20130218/c4a38b58/attachment.bin>


More information about the openconnect-devel mailing list