Skip to content
Snippets Groups Projects
Commit 728a20ee authored by Niels Möller's avatar Niels Möller
Browse files

Avoid calling libc realloc with a requested size of zero.

parent 3a73862a
No related branches found
No related tags found
No related merge requests found
2012-09-16 Niels Möller <nisse@lysator.liu.se>
* realloc.c (nettle_realloc): Only call libc realloc if length >
0, otherwise call free. Fixes a small memory leak.
(nettle_xrealloc): Likewise.
* run-tests (test_program): Don't quote $EMULATOR; allow it to
expand to program and arguments (e.g., valgrind).
......
......@@ -31,20 +31,32 @@
#include "realloc.h"
/* NOTE: Calling libc realloc with size == 0 is not required to
totally free the object, it is allowed to return a valid
pointer. */
void *
nettle_realloc(void *ctx UNUSED, void *p, unsigned length)
{
if (length > 0)
return realloc(p, length);
free(p);
return NULL;
}
void *
nettle_xrealloc(void *ctx UNUSED, void *p, unsigned length)
{
if (length > 0)
{
void *n = realloc(p, length);
if (length && !n)
if (!n)
{
fprintf(stderr, "Virtual memory exhausted.\n");
abort();
}
return n;
}
free(p);
return NULL;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment