From: Bruno Haible <bruno@clisp.org>
Date: Tue, 5 Sep 2006 11:51:57 +0000 (+0000)
Subject: (iconv_alloc): Call iconv with 2xNULL arguments, also to flush the state
X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=5573fef4a678c1cfae0c22c3c4cbce69e983e9c7;p=gnulib.git

(iconv_alloc): Call iconv with 2xNULL arguments, also to flush the state
storage.
---

diff --git a/lib/iconvme.c b/lib/iconvme.c
index 5bf359f4f4..f359e11a84 100644
--- a/lib/iconvme.c
+++ b/lib/iconvme.c
@@ -193,6 +193,48 @@ again:
 	}
     }
 
+again2:
+  err = iconv (cd, NULL, NULL, &outp, &outbytes_remaining);
+
+  if (err == (size_t) -1)
+    {
+      switch (errno)
+	{
+	case E2BIG:
+	  {
+	    size_t used = outp - dest;
+	    size_t newsize = outbuf_size * 2;
+	    char *newdest;
+
+	    if (newsize <= outbuf_size)
+	      {
+		errno = ENOMEM;
+		have_error = 1;
+		goto out;
+	      }
+	    newdest = (char *) realloc (dest, newsize);
+	    if (newdest == NULL)
+	      {
+		errno = ENOMEM;
+		have_error = 1;
+		goto out;
+	      }
+	    dest = newdest;
+	    outbuf_size = newsize;
+
+	    outp = dest + used;
+	    outbytes_remaining = outbuf_size - used - 1;	/* -1 for NUL */
+
+	    goto again2;
+	  }
+	  break;
+
+	default:
+	  have_error = 1;
+	  break;
+	}
+    }
+
   *outp = '\0';
 
 out: