i18n: Properly restart conversion when output buffer overflows.
[pspp] / src / libpspp / i18n.c
index 3735e95d8438821e6b338641581836f83dc7da7c..fa9f29c5835e561354f92877581f0a7ab6f10705 100644 (file)
@@ -117,6 +117,7 @@ recode_string_pool (const char *to, const char *from,
   char *outbuf = 0;
   size_t outbufferlength;
   size_t result;
+  char *ip;
   char *op ;
   size_t inbytes = 0;
   size_t outbytes ;
@@ -142,10 +143,16 @@ recode_string_pool (const char *to, const char *from,
   if ( (iconv_t) -1 == conv )
     return xstrdup (text);
 
+  /* Put the converter into the initial shift state, in case there was any
+     state information left over from its last usage. */
+  iconv (conv, NULL, 0, NULL, 0);
+
   for ( outbufferlength = 1 ; outbufferlength != 0; outbufferlength <<= 1 )
     if ( outbufferlength > length)
       break;
 
+  ip = text;
+
   outbuf = pool_malloc (pool, outbufferlength);
   op = outbuf;
 
@@ -154,8 +161,7 @@ recode_string_pool (const char *to, const char *from,
 
 
   do {
-    const char *ip = text;
-    result = iconv (conv, (ICONV_CONST char **) &text, &inbytes,
+    result = iconv (conv, (ICONV_CONST char **) &ip, &inbytes,
                   &op, &outbytes);
 
     if ( -1 == result )
@@ -176,13 +182,14 @@ recode_string_pool (const char *to, const char *from,
              }
            /* Fall through */
          case E2BIG:
+            iconv (conv, NULL, 0, NULL, 0);
            pool_free (pool, outbuf);
            outbufferlength <<= 1;
            outbuf = pool_malloc (pool, outbufferlength);
            op = outbuf;
            outbytes = outbufferlength;
            inbytes = length;
-           text = ip;
+           ip = text;
            break;
          default:
            /* should never happen */