summaryrefslogtreecommitdiff
path: root/lib/striconveh.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2010-07-13 23:36:41 +0200
committerBruno Haible <bruno@clisp.org>2010-07-13 23:36:41 +0200
commit8099cad631717c6f59d3b1e149971e243eee796e (patch)
tree99ecfe71e47f72d460706af2b652e8e12880aef5 /lib/striconveh.c
parent911a49d5a609b5fcf803d38156eb793e864138a0 (diff)
downloadgnulib-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.c23
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)