bug-gnulib
[Top][All Lists]
Advanced

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

Re: gnulib bugs on OSX & C++


From: Bruno Haible
Subject: Re: gnulib bugs on OSX & C++
Date: Sat, 10 Apr 2010 22:27:16 +0200
User-agent: KMail/1.9.9

> There were two bugs in this area:
>   1) an abort() in a valid use case,
>   2) an endless loop that allocates more and more memory, without bounds.

This fixes the second bug.


2010-04-10  Bruno Haible  <address@hidden>

        vasnprintf: Correct handling of unconvertible wide string arguments.
        * lib/vasnprintf.c (MAX_ROOM_NEEDED): New function, extracted from
        VASNPRINTF.
        (VASNPRINTF): Use it. After snprintf failed, allocate more memory only
        if HAVE_SNPRINTF_RETVAL_C99 is false and the allocated memory is
        smaller than the expected maximum need for the directive. Set errno to
        EILSEQ, not EINVAL, when the directive is 'c' or 's'.
        (local_strnlen, local_wcslen, local_wcsnlen): Update conditions.
        * m4/vasnprintf.m4 (gl_PREREQ_VASNPRINTF): Require AC_C_INLINE and
        gl_SNPRINTF_RETVAL_C99. Define HAVE_SNPRINTF_RETVAL_C99.
        * modules/vasnprintf (Files): Add m4/printf.m4.
        Reported by Jarno Rajahalme <address@hidden>.

--- lib/vasnprintf.c.orig       Sat Apr 10 22:20:33 2010
+++ lib/vasnprintf.c    Sat Apr 10 22:20:21 2010
@@ -207,7 +207,7 @@
 #undef remainder
 #define remainder rem
 
-#if !USE_SNPRINTF && !WIDE_CHAR_VERSION
+#if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && !WIDE_CHAR_VERSION
 # if (HAVE_STRNLEN && !defined _AIX)
 #  define local_strnlen strnlen
 # else
@@ -223,7 +223,7 @@
 # endif
 #endif
 
-#if (!USE_SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && 
HAVE_WCHAR_T && (WIDE_CHAR_VERSION || DCHAR_IS_TCHAR)
+#if (((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && WIDE_CHAR_VERSION) || 
((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && 
!defined IN_LIBINTL)) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)) && HAVE_WCHAR_T
 # if HAVE_WCSLEN
 #  define local_wcslen wcslen
 # else
@@ -246,7 +246,7 @@
 # endif
 #endif
 
-#if !USE_SNPRINTF && HAVE_WCHAR_T && WIDE_CHAR_VERSION
+#if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && HAVE_WCHAR_T && 
WIDE_CHAR_VERSION
 # if HAVE_WCSNLEN
 #  define local_wcsnlen wcsnlen
 # else
@@ -1487,6 +1487,258 @@
 
 #endif
 
+#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99
+
+/* Use a different function name, to make it possible that the 'wchar_t'
+   parametrization and the 'char' parametrization get compiled in the same
+   translation unit.  */
+# if WIDE_CHAR_VERSION
+#  define MAX_ROOM_NEEDED wmax_room_needed
+# else
+#  define MAX_ROOM_NEEDED max_room_needed
+# endif
+
+/* Returns the number of TCHAR_T units needed as temporary space for the result
+   of sprintf or SNPRINTF of a single conversion directive.  */
+static inline size_t
+MAX_ROOM_NEEDED (const arguments *ap, size_t arg_index, FCHAR_T conversion,
+                 arg_type type, int flags, size_t width, int has_precision,
+                 size_t precision, int pad_ourselves)
+{
+  size_t tmp_length;
+
+  switch (conversion)
+    {
+    case 'd': case 'i': case 'u':
+# if HAVE_LONG_LONG_INT
+      if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
+        tmp_length =
+          (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
+                          * 0.30103 /* binary -> decimal */
+                         )
+          + 1; /* turn floor into ceil */
+      else
+# endif
+      if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
+        tmp_length =
+          (unsigned int) (sizeof (unsigned long) * CHAR_BIT
+                          * 0.30103 /* binary -> decimal */
+                         )
+          + 1; /* turn floor into ceil */
+      else
+        tmp_length =
+          (unsigned int) (sizeof (unsigned int) * CHAR_BIT
+                          * 0.30103 /* binary -> decimal */
+                         )
+          + 1; /* turn floor into ceil */
+      if (tmp_length < precision)
+        tmp_length = precision;
+      /* Multiply by 2, as an estimate for FLAG_GROUP.  */
+      tmp_length = xsum (tmp_length, tmp_length);
+      /* Add 1, to account for a leading sign.  */
+      tmp_length = xsum (tmp_length, 1);
+      break;
+
+    case 'o':
+# if HAVE_LONG_LONG_INT
+      if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
+        tmp_length =
+          (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
+                          * 0.333334 /* binary -> octal */
+                         )
+          + 1; /* turn floor into ceil */
+      else
+# endif
+      if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
+        tmp_length =
+          (unsigned int) (sizeof (unsigned long) * CHAR_BIT
+                          * 0.333334 /* binary -> octal */
+                         )
+          + 1; /* turn floor into ceil */
+      else
+        tmp_length =
+          (unsigned int) (sizeof (unsigned int) * CHAR_BIT
+                          * 0.333334 /* binary -> octal */
+                         )
+          + 1; /* turn floor into ceil */
+      if (tmp_length < precision)
+        tmp_length = precision;
+      /* Add 1, to account for a leading sign.  */
+      tmp_length = xsum (tmp_length, 1);
+      break;
+
+    case 'x': case 'X':
+# if HAVE_LONG_LONG_INT
+      if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
+        tmp_length =
+          (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
+                          * 0.25 /* binary -> hexadecimal */
+                         )
+          + 1; /* turn floor into ceil */
+      else
+# endif
+      if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
+        tmp_length =
+          (unsigned int) (sizeof (unsigned long) * CHAR_BIT
+                          * 0.25 /* binary -> hexadecimal */
+                         )
+          + 1; /* turn floor into ceil */
+      else
+        tmp_length =
+          (unsigned int) (sizeof (unsigned int) * CHAR_BIT
+                          * 0.25 /* binary -> hexadecimal */
+                         )
+          + 1; /* turn floor into ceil */
+      if (tmp_length < precision)
+        tmp_length = precision;
+      /* Add 2, to account for a leading sign or alternate form.  */
+      tmp_length = xsum (tmp_length, 2);
+      break;
+
+    case 'f': case 'F':
+      if (type == TYPE_LONGDOUBLE)
+        tmp_length =
+          (unsigned int) (LDBL_MAX_EXP
+                          * 0.30103 /* binary -> decimal */
+                          * 2 /* estimate for FLAG_GROUP */
+                         )
+          + 1 /* turn floor into ceil */
+          + 10; /* sign, decimal point etc. */
+      else
+        tmp_length =
+          (unsigned int) (DBL_MAX_EXP
+                          * 0.30103 /* binary -> decimal */
+                          * 2 /* estimate for FLAG_GROUP */
+                         )
+          + 1 /* turn floor into ceil */
+          + 10; /* sign, decimal point etc. */
+      tmp_length = xsum (tmp_length, precision);
+      break;
+
+    case 'e': case 'E': case 'g': case 'G':
+      tmp_length =
+        12; /* sign, decimal point, exponent etc. */
+      tmp_length = xsum (tmp_length, precision);
+      break;
+
+    case 'a': case 'A':
+      if (type == TYPE_LONGDOUBLE)
+        tmp_length =
+          (unsigned int) (LDBL_DIG
+                          * 0.831 /* decimal -> hexadecimal */
+                         )
+          + 1; /* turn floor into ceil */
+      else
+        tmp_length =
+          (unsigned int) (DBL_DIG
+                          * 0.831 /* decimal -> hexadecimal */
+                         )
+          + 1; /* turn floor into ceil */
+      if (tmp_length < precision)
+        tmp_length = precision;
+      /* Account for sign, decimal point etc. */
+      tmp_length = xsum (tmp_length, 12);
+      break;
+
+    case 'c':
+# if HAVE_WINT_T && !WIDE_CHAR_VERSION
+      if (type == TYPE_WIDE_CHAR)
+        tmp_length = MB_CUR_MAX;
+      else
+# endif
+        tmp_length = 1;
+      break;
+
+    case 's':
+# if HAVE_WCHAR_T
+      if (type == TYPE_WIDE_STRING)
+        {
+#  if WIDE_CHAR_VERSION
+          /* ISO C says about %ls in fwprintf:
+               "If the precision is not specified or is greater than the size
+                of the array, the array shall contain a null wide character."
+             So if there is a precision, we must not use wcslen.  */
+          const wchar_t *arg = ap->arg[arg_index].a.a_wide_string;
+
+          if (has_precision)
+            tmp_length = local_wcsnlen (arg, precision);
+          else
+            tmp_length = local_wcslen (arg);
+#  else
+          /* ISO C says about %ls in fprintf:
+               "If a precision is specified, no more than that many bytes are
+                written (including shift sequences, if any), and the array
+                shall contain a null wide character if, to equal the multibyte
+                character sequence length given by the precision, the function
+                would need to access a wide character one past the end of the
+                array."
+             So if there is a precision, we must not use wcslen.  */
+          /* This case has already been handled separately in VASNPRINTF.  */
+          abort ();
+#  endif
+        }
+      else
+# endif
+        {
+# if WIDE_CHAR_VERSION
+          /* ISO C says about %s in fwprintf:
+               "If the precision is not specified or is greater than the size
+                of the converted array, the converted array shall contain a
+                null wide character."
+             So if there is a precision, we must not use strlen.  */
+          /* This case has already been handled separately in VASNPRINTF.  */
+          abort ();
+# else
+          /* ISO C says about %s in fprintf:
+               "If the precision is not specified or greater than the size of
+                the array, the array shall contain a null character."
+             So if there is a precision, we must not use strlen.  */
+          const char *arg = ap->arg[arg_index].a.a_string;
+
+          if (has_precision)
+            tmp_length = local_strnlen (arg, precision);
+          else
+            tmp_length = strlen (arg);
+# endif
+        }
+      break;
+
+    case 'p':
+      tmp_length =
+        (unsigned int) (sizeof (void *) * CHAR_BIT
+                        * 0.25 /* binary -> hexadecimal */
+                       )
+          + 1 /* turn floor into ceil */
+          + 2; /* account for leading 0x */
+      break;
+
+    default:
+      abort ();
+    }
+
+  if (!pad_ourselves)
+    {
+# if ENABLE_UNISTDIO
+      /* Padding considers the number of characters, therefore the number of
+         elements after padding may be
+           > max (tmp_length, width)
+         but is certainly
+           <= tmp_length + width.  */
+      tmp_length = xsum (tmp_length, width);
+# else
+      /* Padding considers the number of elements, says POSIX.  */
+      if (tmp_length < width)
+        tmp_length = width;
+# endif
+    }
+
+  tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
+
+  return tmp_length;
+}
+
+#endif
+
 DCHAR_T *
 VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
             const FCHAR_T *format, va_list args)
@@ -2116,7 +2368,7 @@
                   }
               }
 #endif
-#if (!USE_SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && 
HAVE_WCHAR_T
+#if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS 
&& !defined IN_LIBINTL)) && HAVE_WCHAR_T
             else if (dp->conversion == 's'
 # if WIDE_CHAR_VERSION
                      && a.arg[dp->arg_index].type != TYPE_WIDE_STRING
@@ -4322,11 +4574,11 @@
               {
                 arg_type type = a.arg[dp->arg_index].type;
                 int flags = dp->flags;
-#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || 
NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || 
NEED_PRINTF_UNBOUNDED_PRECISION
+#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || 
ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || 
NEED_PRINTF_UNBOUNDED_PRECISION
                 int has_width;
                 size_t width;
 #endif
-#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
+#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || 
NEED_PRINTF_UNBOUNDED_PRECISION
                 int has_precision;
                 size_t precision;
 #endif
@@ -4351,7 +4603,7 @@
                 TCHAR_T *tmp;
 #endif
 
-#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || 
NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || 
NEED_PRINTF_UNBOUNDED_PRECISION
+#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || 
ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || 
NEED_PRINTF_UNBOUNDED_PRECISION
                 has_width = 0;
                 width = 0;
                 if (dp->width_start != dp->width_end)
@@ -4385,7 +4637,7 @@
                   }
 #endif
 
-#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
+#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || 
NEED_PRINTF_UNBOUNDED_PRECISION
                 has_precision = 0;
                 precision = 6;
                 if (dp->precision_start != dp->precision_end)
@@ -4458,246 +4710,10 @@
 #if !USE_SNPRINTF
                 /* Allocate a temporary buffer of sufficient size for calling
                    sprintf.  */
-                {
-                  switch (dp->conversion)
-                    {
-
-                    case 'd': case 'i': case 'u':
-# if HAVE_LONG_LONG_INT
-                      if (type == TYPE_LONGLONGINT || type == 
TYPE_ULONGLONGINT)
-                        tmp_length =
-                          (unsigned int) (sizeof (unsigned long long) * 
CHAR_BIT
-                                          * 0.30103 /* binary -> decimal */
-                                         )
-                          + 1; /* turn floor into ceil */
-                      else
-# endif
-                      if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
-                        tmp_length =
-                          (unsigned int) (sizeof (unsigned long) * CHAR_BIT
-                                          * 0.30103 /* binary -> decimal */
-                                         )
-                          + 1; /* turn floor into ceil */
-                      else
-                        tmp_length =
-                          (unsigned int) (sizeof (unsigned int) * CHAR_BIT
-                                          * 0.30103 /* binary -> decimal */
-                                         )
-                          + 1; /* turn floor into ceil */
-                      if (tmp_length < precision)
-                        tmp_length = precision;
-                      /* Multiply by 2, as an estimate for FLAG_GROUP.  */
-                      tmp_length = xsum (tmp_length, tmp_length);
-                      /* Add 1, to account for a leading sign.  */
-                      tmp_length = xsum (tmp_length, 1);
-                      break;
-
-                    case 'o':
-# if HAVE_LONG_LONG_INT
-                      if (type == TYPE_LONGLONGINT || type == 
TYPE_ULONGLONGINT)
-                        tmp_length =
-                          (unsigned int) (sizeof (unsigned long long) * 
CHAR_BIT
-                                          * 0.333334 /* binary -> octal */
-                                         )
-                          + 1; /* turn floor into ceil */
-                      else
-# endif
-                      if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
-                        tmp_length =
-                          (unsigned int) (sizeof (unsigned long) * CHAR_BIT
-                                          * 0.333334 /* binary -> octal */
-                                         )
-                          + 1; /* turn floor into ceil */
-                      else
-                        tmp_length =
-                          (unsigned int) (sizeof (unsigned int) * CHAR_BIT
-                                          * 0.333334 /* binary -> octal */
-                                         )
-                          + 1; /* turn floor into ceil */
-                      if (tmp_length < precision)
-                        tmp_length = precision;
-                      /* Add 1, to account for a leading sign.  */
-                      tmp_length = xsum (tmp_length, 1);
-                      break;
-
-                    case 'x': case 'X':
-# if HAVE_LONG_LONG_INT
-                      if (type == TYPE_LONGLONGINT || type == 
TYPE_ULONGLONGINT)
-                        tmp_length =
-                          (unsigned int) (sizeof (unsigned long long) * 
CHAR_BIT
-                                          * 0.25 /* binary -> hexadecimal */
-                                         )
-                          + 1; /* turn floor into ceil */
-                      else
-# endif
-                      if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
-                        tmp_length =
-                          (unsigned int) (sizeof (unsigned long) * CHAR_BIT
-                                          * 0.25 /* binary -> hexadecimal */
-                                         )
-                          + 1; /* turn floor into ceil */
-                      else
-                        tmp_length =
-                          (unsigned int) (sizeof (unsigned int) * CHAR_BIT
-                                          * 0.25 /* binary -> hexadecimal */
-                                         )
-                          + 1; /* turn floor into ceil */
-                      if (tmp_length < precision)
-                        tmp_length = precision;
-                      /* Add 2, to account for a leading sign or alternate 
form.  */
-                      tmp_length = xsum (tmp_length, 2);
-                      break;
-
-                    case 'f': case 'F':
-                      if (type == TYPE_LONGDOUBLE)
-                        tmp_length =
-                          (unsigned int) (LDBL_MAX_EXP
-                                          * 0.30103 /* binary -> decimal */
-                                          * 2 /* estimate for FLAG_GROUP */
-                                         )
-                          + 1 /* turn floor into ceil */
-                          + 10; /* sign, decimal point etc. */
-                      else
-                        tmp_length =
-                          (unsigned int) (DBL_MAX_EXP
-                                          * 0.30103 /* binary -> decimal */
-                                          * 2 /* estimate for FLAG_GROUP */
-                                         )
-                          + 1 /* turn floor into ceil */
-                          + 10; /* sign, decimal point etc. */
-                      tmp_length = xsum (tmp_length, precision);
-                      break;
-
-                    case 'e': case 'E': case 'g': case 'G':
-                      tmp_length =
-                        12; /* sign, decimal point, exponent etc. */
-                      tmp_length = xsum (tmp_length, precision);
-                      break;
-
-                    case 'a': case 'A':
-                      if (type == TYPE_LONGDOUBLE)
-                        tmp_length =
-                          (unsigned int) (LDBL_DIG
-                                          * 0.831 /* decimal -> hexadecimal */
-                                         )
-                          + 1; /* turn floor into ceil */
-                      else
-                        tmp_length =
-                          (unsigned int) (DBL_DIG
-                                          * 0.831 /* decimal -> hexadecimal */
-                                         )
-                          + 1; /* turn floor into ceil */
-                      if (tmp_length < precision)
-                        tmp_length = precision;
-                      /* Account for sign, decimal point etc. */
-                      tmp_length = xsum (tmp_length, 12);
-                      break;
-
-                    case 'c':
-# if HAVE_WINT_T && !WIDE_CHAR_VERSION
-                      if (type == TYPE_WIDE_CHAR)
-                        tmp_length = MB_CUR_MAX;
-                      else
-# endif
-                        tmp_length = 1;
-                      break;
-
-                    case 's':
-# if HAVE_WCHAR_T
-                      if (type == TYPE_WIDE_STRING)
-                        {
-#  if WIDE_CHAR_VERSION
-                          /* ISO C says about %ls in fwprintf:
-                               "If the precision is not specified or is greater
-                                than the size of the array, the array shall
-                                contain a null wide character."
-                             So if there is a precision, we must not use
-                             wcslen.  */
-                          const wchar_t *arg =
-                            a.arg[dp->arg_index].a.a_wide_string;
-
-                          if (has_precision)
-                            tmp_length = local_wcsnlen (arg, precision);
-                          else
-                            tmp_length = local_wcslen (arg);
-#  else
-                          /* ISO C says about %ls in fprintf:
-                               "If a precision is specified, no more than that
-                                many bytes are written (including shift
-                                sequences, if any), and the array shall contain
-                                a null wide character if, to equal the
-                                multibyte character sequence length given by
-                                the precision, the function would need to
-                                access a wide character one past the end of the
-                                array."
-                             So if there is a precision, we must not use
-                             wcslen.  */
-                          /* This case has already been handled above.  */
-                          abort ();
-#  endif
-                        }
-                      else
-# endif
-                        {
-# if WIDE_CHAR_VERSION
-                          /* ISO C says about %s in fwprintf:
-                               "If the precision is not specified or is greater
-                                than the size of the converted array, the
-                                converted array shall contain a null wide
-                                character."
-                             So if there is a precision, we must not use
-                             strlen.  */
-                          /* This case has already been handled above.  */
-                          abort ();
-# else
-                          /* ISO C says about %s in fprintf:
-                               "If the precision is not specified or greater
-                                than the size of the array, the array shall
-                                contain a null character."
-                             So if there is a precision, we must not use
-                             strlen.  */
-                          const char *arg = a.arg[dp->arg_index].a.a_string;
-
-                          if (has_precision)
-                            tmp_length = local_strnlen (arg, precision);
-                          else
-                            tmp_length = strlen (arg);
-# endif
-                        }
-                      break;
-
-                    case 'p':
-                      tmp_length =
-                        (unsigned int) (sizeof (void *) * CHAR_BIT
-                                        * 0.25 /* binary -> hexadecimal */
-                                       )
-                          + 1 /* turn floor into ceil */
-                          + 2; /* account for leading 0x */
-                      break;
-
-                    default:
-                      abort ();
-                    }
-
-                  if (!pad_ourselves)
-                    {
-# if ENABLE_UNISTDIO
-                      /* Padding considers the number of characters, therefore
-                         the number of elements after padding may be
-                           > max (tmp_length, width)
-                         but is certainly
-                           <= tmp_length + width.  */
-                      tmp_length = xsum (tmp_length, width);
-# else
-                      /* Padding considers the number of elements,
-                         says POSIX.  */
-                      if (tmp_length < width)
-                        tmp_length = width;
-# endif
-                    }
-
-                  tmp_length = xsum (tmp_length, 1); /* account for trailing 
NUL */
-                }
+                tmp_length =
+                  MAX_ROOM_NEEDED (&a, dp->arg_index, dp->conversion, type,
+                                   flags, width, has_precision, precision,
+                                   pad_ourselves);
 
                 if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
                   tmp = tmpbuf;
@@ -5083,15 +5099,44 @@
                             /* Look at the snprintf() return value.  */
                             if (retcount < 0)
                               {
+# if !HAVE_SNPRINTF_RETVAL_C99
                                 /* HP-UX 10.20 snprintf() is doubly deficient:
                                    It doesn't understand the '%n' directive,
                                    *and* it returns -1 (rather than the length
                                    that would have been required) when the
-                                   buffer is too small.  */
-                                size_t bigger_need =
-                                  xsum (xtimes (allocated, 2), 12);
-                                ENSURE_ALLOCATION (bigger_need);
-                                continue;
+                                   buffer is too small.
+                                   But a failure at this point can also come
+                                   from other reasons than a too small buffer,
+                                   such as an invalid wide string argument to
+                                   the %ls directive, or possibly an invalid
+                                   floating-point argument.  */
+                                size_t tmp_length =
+                                  MAX_ROOM_NEEDED (&a, dp->arg_index,
+                                                   dp->conversion, type, flags,
+                                                   width, has_precision,
+                                                   precision, pad_ourselves);
+
+                                if (maxlen < tmp_length)
+                                  {
+                                    /* Make more room.  But try to do through
+                                       this reallocation only once.  */
+                                    size_t bigger_need =
+                                      xsum (length,
+                                            xsum (tmp_length,
+                                                  TCHARS_PER_DCHAR - 1)
+                                            / TCHARS_PER_DCHAR);
+                                    /* And always grow proportionally.
+                                       (There may be several arguments, each
+                                       needing a little more room than the
+                                       previous one.)  */
+                                    size_t bigger_need2 =
+                                      xsum (xtimes (allocated, 2), 12);
+                                    if (bigger_need < bigger_need2)
+                                      bigger_need = bigger_need2;
+                                    ENSURE_ALLOCATION (bigger_need);
+                                    continue;
+                                  }
+# endif
                               }
                             else
                               count = retcount;
@@ -5107,7 +5152,10 @@
                         if (buf_malloced != NULL)
                           free (buf_malloced);
                         CLEANUP ();
-                        errno = EINVAL;
+                        errno =
+                          (dp->conversion == 'c' || dp->conversion == 's'
+                           ? EILSEQ
+                           : EINVAL);
                         return NULL;
                       }
 
@@ -5494,6 +5542,7 @@
   }
 }
 
+#undef MAX_ROOM_NEEDED
 #undef TCHARS_PER_DCHAR
 #undef SNPRINTF
 #undef USE_SNPRINTF
--- m4/vasnprintf.m4.orig       Sat Apr 10 22:20:34 2010
+++ m4/vasnprintf.m4    Sat Apr 10 22:20:20 2010
@@ -1,4 +1,4 @@
-# vasnprintf.m4 serial 29
+# vasnprintf.m4 serial 31
 dnl Copyright (C) 2002-2004, 2006-2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -54,6 +54,7 @@
 # Prerequisites of lib/vasnprintf.c.
 AC_DEFUN_ONCE([gl_PREREQ_VASNPRINTF],
 [
+  AC_REQUIRE([AC_C_INLINE])
   AC_REQUIRE([AC_FUNC_ALLOCA])
   AC_REQUIRE([AC_TYPE_LONG_LONG_INT])
   AC_REQUIRE([gt_TYPE_WCHAR_T])
@@ -62,6 +63,17 @@
   dnl Use the _snprintf function only if it is declared (because on NetBSD it
   dnl is defined as a weak alias of snprintf; we prefer to use the latter).
   AC_CHECK_DECLS([_snprintf], , , [#include <stdio.h>])
+  dnl We can avoid a lot of code by assuming that snprintf's return value
+  dnl conforms to ISO C99. So check that.
+  AC_REQUIRE([gl_SNPRINTF_RETVAL_C99])
+  case "$gl_cv_func_snprintf_retval_c99" in
+    *yes)
+      AC_DEFINE([HAVE_SNPRINTF_RETVAL_C99], [1],
+        [Define if the return value of the snprintf function is the number of
+         of bytes (excluding the terminating NUL) that would have been produced
+         if the buffer had been large enough.])
+      ;;
+  esac
 ])
 
 # Extra prerequisites of lib/vasnprintf.c for supporting 'long double'
--- modules/vasnprintf.orig     Sat Apr 10 22:20:34 2010
+++ modules/vasnprintf  Sat Apr 10 22:20:20 2010
@@ -17,6 +17,7 @@
 m4/stdint_h.m4
 m4/inttypes_h.m4
 m4/vasnprintf.m4
+m4/printf.m4
 
 Depends-on:
 alloca-opt




reply via email to

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