From 4c8e0be3ce3cc152b3630086e5e5a1ad7fc5b27c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= Date: Mon, 11 Nov 2013 17:29:00 +0100 Subject: [PATCH] Free memory on error in simple_recode() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Petr Písař --- simple_recode.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/simple_recode.c b/simple_recode.c index 0893df4..97164fb 100644 --- a/simple_recode.c +++ b/simple_recode.c @@ -94,6 +94,7 @@ char *simple_recode(const iconv_t handle, const char *str) { size_t used = outp - result; size_t newsize; + char *new_result; if (outbuf_size < SIMPLE_RECODE_BUFFER_SIZE_2) newsize = SIMPLE_RECODE_BUFFER_SIZE_2; @@ -103,13 +104,17 @@ char *simple_recode(const iconv_t handle, const char *str) /* check if the newsize variable has overflowed */ if (newsize <= outbuf_size) { + free(result); errno = ENOMEM; return NULL; } outbuf_size = newsize; - result = realloc(result, outbuf_size); - if (!result) + new_result = realloc(result, outbuf_size); + if (!new_result) { + free(result); return NULL; + } + result = new_result; /* update the position in the new output stream */ outp = result + used; @@ -119,6 +124,7 @@ char *simple_recode(const iconv_t handle, const char *str) } default: + free(result); return NULL; } } while (inbytes_remaining > 0);