changeset 13465:dc342fd2f695

striconveh: Don't malloc memory if the result buffer is sufficient.
author Bruno Haible <bruno@clisp.org>
date Tue, 13 Jul 2010 23:36:41 +0200
parents abaeec5957d0
children 00760b212392
files ChangeLog lib/striconveh.c
diffstat 2 files changed, 23 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- a/lib/striconveh.c
+++ b/lib/striconveh.c
@@ -970,18 +970,27 @@
   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)