Fix detection of overflow: don't assume that snprintf is C99 compliant.
authorBruno Haible <bruno@clisp.org>
Sat, 3 Nov 2007 14:22:52 +0000 (15:22 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 3 Nov 2007 14:22:52 +0000 (15:22 +0100)
ChangeLog
lib/vasnprintf.c

index 15cd4c705999d3d8858e8bfa332329af768e5d5b..6c965e410805ea5f92ef1797ba2720b928aeeeae 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-11-03  Bruno Haible  <bruno@clisp.org>
+
+       * lib/vasnprintf.c (VASNPRINTF): Don't assume that snprintf's return
+       value is C99 compliant.
+       Needed for OSF/1 5.1.
+
 2007-11-03  Bruno Haible  <bruno@clisp.org>
 
        Fix out-of-memory handling of vasnprintf.
index f081e3adeff180dc3098b93f45db94edefb1cb8e..6b4d6c6a519a70c4f068d228139a27af6ff573bc 100644 (file)
@@ -3658,8 +3658,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
                      }
 
 #if USE_SNPRINTF
-                   /* Handle overflow of the allocated buffer.  */
-                   if (count >= maxlen)
+                   /* Handle overflow of the allocated buffer.
+                      If such an overflow occurs, a C99 compliant snprintf()
+                      returns a count >= maxlen.  However, a non-compliant
+                      snprintf() function returns only count = maxlen - 1.  To
+                      cover both cases, test whether count >= maxlen - 1.  */
+                   if ((unsigned int) count + 1 >= maxlen)
                      {
                        /* If maxlen already has attained its allowed maximum,
                           allocating more memory will not increase maxlen.