From 8099cad631717c6f59d3b1e149971e243eee796e Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 13 Jul 2010 23:36:41 +0200 Subject: [PATCH] striconveh: Don't malloc memory if the result buffer is sufficient. --- ChangeLog | 7 +++++++ lib/striconveh.c | 23 ++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 10d49b197..3ffe6b5a3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2010-07-13 Bruno Haible + striconveh: Don't malloc memory if the result buffer is sufficient. + * lib/striconveh.c (mem_cd_iconveh_internal): Use the provided result + buffer if its size is sufficient. + Reported by Ludovic Courtès . + +2010-07-13 Bruno Haible + strtod: Add safety check. * lib/strtod.c (ldexp): Abort if this dummy replacement gets called. diff --git a/lib/striconveh.c b/lib/striconveh.c index e448a994d..9467e4dbd 100644 --- a/lib/striconveh.c +++ b/lib/striconveh.c @@ -970,18 +970,27 @@ mem_cd_iconveh_internal (const char *src, size_t srclen, if (result == tmpbuf) { size_t memsize = length + extra_alloc; - char *memory; - memory = (char *) malloc (memsize > 0 ? memsize : 1); - if (memory != NULL) + if (*resultp != NULL && *lengthp >= memsize) { - memcpy (memory, tmpbuf, length); - result = memory; + result = *resultp; + memcpy (result, tmpbuf, length); } else { - errno = ENOMEM; - return -1; + char *memory; + + memory = (char *) malloc (memsize > 0 ? memsize : 1); + if (memory != NULL) + { + memcpy (memory, tmpbuf, length); + result = memory; + } + else + { + errno = ENOMEM; + return -1; + } } } else if (result != *resultp && length + extra_alloc < allocated) -- 2.11.0