Avoid failure when attempting to return empty iconv results on some platforms.
authorBruno Haible <bruno@clisp.org>
Sun, 30 Mar 2008 16:48:14 +0000 (18:48 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 30 Mar 2008 16:48:14 +0000 (18:48 +0200)
ChangeLog
lib/striconveh.c

index 3388d1665999ffe69b7492505f72677c450ce184..76a695ff203157e9a2810ef4b124aea74647710a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2008-03-30  Bruno Haible  <bruno@clisp.org>
+
+       Avoid failure when attempting to return empty iconv results on some
+       platforms.
+       * lib/striconveh.c (mem_cd_iconveh_internal): In the final memory
+       allocation, don't report ENOMEM when the resulting string is empty.
+
 2008-03-30  Bruno Haible  <bruno@clisp.org>
 
        Fix buffer overrun.
index 99bbe18d29eca1caacee330c49b2fe588db79dc5..761f3b115b906b18b80d1ef1f9f22bd7f049cd60 100644 (file)
@@ -1,5 +1,5 @@
 /* Character set conversion with error handling.
-   Copyright (C) 2001-2007 Free Software Foundation, Inc.
+   Copyright (C) 2001-2008 Free Software Foundation, Inc.
    Written by Bruno Haible and Simon Josefsson.
 
    This program is free software: you can redistribute it and/or modify
@@ -870,9 +870,10 @@ mem_cd_iconveh_internal (const char *src, size_t srclen,
   /* Now the final memory allocation.  */
   if (result == tmpbuf)
     {
+      size_t memsize = length + extra_alloc;
       char *memory;
 
-      memory = (char *) malloc (length + extra_alloc);
+      memory = (char *) malloc (memsize > 0 ? memsize : 1);
       if (memory != NULL)
        {
          memcpy (memory, tmpbuf, length);
@@ -887,9 +888,10 @@ mem_cd_iconveh_internal (const char *src, size_t srclen,
   else if (result != *resultp && length + extra_alloc < allocated)
     {
       /* Shrink the allocated memory if possible.  */
+      size_t memsize = length + extra_alloc;
       char *memory;
 
-      memory = (char *) realloc (result, length + extra_alloc);
+      memory = (char *) realloc (result, memsize > 0 ? memsize : 1);
       if (memory != NULL)
        result = memory;
     }