striconveh: Don't malloc memory if the result buffer is sufficient.
authorBruno Haible <bruno@clisp.org>
Tue, 13 Jul 2010 21:36:41 +0000 (23:36 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 13 Jul 2010 21:36:41 +0000 (23:36 +0200)
ChangeLog
lib/striconveh.c

index 10d49b197eb6f4da5ee8a0a3a5c45b145b99f843..3ffe6b5a3e861bceb35d59eeda34cb729dd296fc 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2010-07-13  Bruno Haible  <bruno@clisp.org>
+
+       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 <ludo@gnu.org>.
+
 2010-07-13  Bruno Haible  <bruno@clisp.org>
 
        strtod: Add safety check.
index e448a994d6063acf7ef1b1ebe04cd908feb4586b..9467e4dbd778b525f7dbd925335c6f2221a696c8 100644 (file)
@@ -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)