bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gnulib] Re: iconvme again


From: Paul Eggert
Subject: Re: [bug-gnulib] Re: iconvme again
Date: Thu, 24 Feb 2005 00:16:17 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux)

Stepan Kasal <address@hidden> writes:

> Let me repeat what Jakub said in the bugzilla: even in the case of
> arithmetic overflow, we should try to perform conversion.

Yes, that makes sense.  How about this patch?  It's relative to
gnulib.  It adds a comment describing this situation.  (But it is
uncompiled and untested.  :-)


--- iconvme.c.~1.1.~    2005-02-23 15:30:10 -0800
+++ iconvme.c   2005-02-24 00:11:21 -0800
@@ -62,17 +62,14 @@ iconv_string (const char *str, const cha
   char *outp;
   char *p = (char *) str;
   size_t inbytes_remaining = strlen (p);
-  /* Guess the maximum length the output string can have.  */
-  size_t outbuf_size = (inbytes_remaining + 1) * MB_LEN_MAX;
-  size_t outbytes_remaining = outbuf_size - 1; /* -1 for NUL */
   size_t err;
   int have_error = 0;
 
-  if (1 < MB_LEN_MAX && SIZE_MAX / MB_LEN_MAX <= inbytes_remaining)
-    {
-      errno = ENOMEM;
-      return NULL;
-    }
+  /* Guess the output buffer size.  It does not affect correctness if
+     the guess is wrong (e.g., due to arithmetic overflow) so long as
+     the guess is not zero.  */
+  size_t outbuf_size = (inbytes_remaining + 1) * MB_LEN_MAX;
+  outbuf_size += (outbuf_size == 0);
 #endif
 
   if (strcmp (to_codeset, from_codeset) == 0)
@@ -88,7 +85,10 @@ iconv_string (const char *str, const cha
     goto out;
 
 again:
-  err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
+  {
+    size_t outbytes_remaining = outbuf_size - (outp - dest) - 1;
+    err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
+  }
 
   if (err == (size_t) - 1)
     {
@@ -120,8 +120,6 @@ again:
            outbuf_size = newsize;
 
            outp = dest + used;
-           outbytes_remaining = outbuf_size - used - 1;        /* -1 for NUL */
-
            goto again;
          }
          break;






reply via email to

[Prev in Thread] Current Thread [Next in Thread]