diff options
author | Bruno Haible <bruno@clisp.org> | 2010-07-13 23:36:41 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2010-07-13 23:36:41 +0200 |
commit | 8099cad631717c6f59d3b1e149971e243eee796e (patch) | |
tree | 99ecfe71e47f72d460706af2b652e8e12880aef5 /lib/striconveh.c | |
parent | 911a49d5a609b5fcf803d38156eb793e864138a0 (diff) | |
download | gnulib-8099cad631717c6f59d3b1e149971e243eee796e.tar.gz |
striconveh: Don't malloc memory if the result buffer is sufficient.
Diffstat (limited to 'lib/striconveh.c')
-rw-r--r-- | lib/striconveh.c | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/lib/striconveh.c b/lib/striconveh.c index e448a994d6..9467e4dbd7 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) |