From 18c07ec5a335626b333c5146137851e1b59e6495 Mon Sep 17 00:00:00 2001 From: Danny Al-Gaaf Date: Wed, 4 Jun 2014 22:33:51 +0200 Subject: [PATCH] common/addr_parsing.c: fix realloc memory leak Fix handling of realloc. If realloc() fails it returns NULL, assigning the return value of realloc() directly to the pointer without checking for the result will lead to a memory leak in error case. Use a temporary pointer to hold the result of realloc(). In error case print error and exit, otherwise assign it to the pointer we want to realloc. Signed-off-by: Danny Al-Gaaf --- src/common/addr_parsing.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/common/addr_parsing.c b/src/common/addr_parsing.c index c01f817772c75..d50b10eef4538 100644 --- a/src/common/addr_parsing.c +++ b/src/common/addr_parsing.c @@ -31,11 +31,13 @@ int safe_cat(char **pstr, int *plen, int pos, const char *str2) //printf("safe_cat '%s' max %d pos %d '%s' len %d\n", *pstr, *plen, pos, str2, len2); while (*plen < pos + len2 + 1) { *plen += BUF_SIZE; - *pstr = (char *)realloc(*pstr, (size_t)*plen); - if (!*pstr) { + void *_realloc = NULL; + if ((_realloc = realloc(*pstr, (size_t)*plen)) == NULL) { printf("Out of memory\n"); exit(1); + } else { + *pstr = (char *)_realloc; } //printf("safe_cat '%s' max %d pos %d '%s' len %d\n", *pstr, *plen, pos, str2, len2); } -- 2.39.5