Don't report an unjustified overflow error.
authorBruno Haible <bruno@clisp.org>
Sat, 20 Oct 2007 12:56:19 +0000 (14:56 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 20 Oct 2007 12:56:19 +0000 (14:56 +0200)
ChangeLog
lib/vasnprintf.c

index d3f95b760c99495dbc409c8535475b6309791c48..96a9a09bdfff5ccd197ad15f1e069f5e87f12677 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-10-20  Bruno Haible  <bruno@clisp.org>
+
+       * lib/vasnprintf.c (VASNPRINTF): Don't report overflow if the available
+       length is INT_MAX and sizeof (DCHAR_T) > sizeof (TCHAR_T).
+       Reported by Ralf Wildenhues <Ralf.Wildenhues@gmx.de>.
+
 2007-10-20  Bruno Haible  <bruno@clisp.org>
 
        * tests/test-floorf2.c (correct_result_p): Don't rely on excess
index 5d818aa6424919f9b02fa385247dad51b4a0c99a..42d0e747a77992921a22e4a1897164b4390b94b7 100644 (file)
@@ -3442,7 +3442,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
                    /* SNPRINTF can fail if its second argument is
                       > INT_MAX.  */
                    if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
-                     goto overflow;
+                     maxlen = INT_MAX / TCHARS_PER_DCHAR;
                    maxlen = maxlen * TCHARS_PER_DCHAR;
 # define SNPRINTF_BUF(arg) \
                    switch (prefix_count)                                   \
@@ -3663,17 +3663,26 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
                    /* Handle overflow of the allocated buffer.  */
                    if (count >= maxlen)
                      {
-                       /* Need at least count * sizeof (TCHAR_T) bytes.  But
-                          allocate proportionally, to avoid looping eternally
-                          if snprintf() reports a too small count.  */
-                       size_t n =
-                         xmax (xsum (length,
-                                     (count + TCHARS_PER_DCHAR - 1)
-                                     / TCHARS_PER_DCHAR),
-                               xtimes (allocated, 2));
-
-                       ENSURE_ALLOCATION (n);
-                       continue;
+                       /* If maxlen already has attained its allowed maximum,
+                          allocating more memory will not increase maxlen.
+                          Instead of looping, bail out.  */
+                       if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
+                         goto overflow;
+                       else
+                         {
+                           /* Need at least count * sizeof (TCHAR_T) bytes.
+                              But allocate proportionally, to avoid looping
+                              eternally if snprintf() reports a too small
+                              count.  */
+                           size_t n =
+                             xmax (xsum (length,
+                                         (count + TCHARS_PER_DCHAR - 1)
+                                         / TCHARS_PER_DCHAR),
+                                   xtimes (allocated, 2));
+
+                           ENSURE_ALLOCATION (n);
+                           continue;
+                         }
                      }
 #endif