]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
- one IRIX an unsigned size_t creates problems when we do (len-1) in snprintf
authorrousskov <>
Fri, 28 Aug 1998 04:00:07 +0000 (04:00 +0000)
committerrousskov <>
Fri, 28 Aug 1998 04:00:07 +0000 (04:00 +0000)
  fixed by cheking that len is positive before subtracting 1.
- return value was sometimes uninitialized when calling snprintf

lib/snprintf.c

index c3c31f3cfffa7b0333283dce1c810f0dcdb89d8c..d7edcbe1f03ac8b62b873b734164c89a4c322d3e 100644 (file)
@@ -880,6 +880,8 @@ format_converter(register buffy * odp, const char *fmt,
 
 /*
  * This is the general purpose conversion function.
+ * Must be called with len >= 0, but we cannot assert() that
+ * because size_t is unsigned on some platforms
  */
 static void
 strx_printv(int *ccp, char *buf, size_t len, const char *format,
@@ -888,14 +890,6 @@ strx_printv(int *ccp, char *buf, size_t len, const char *format,
     buffy od;
     int cc;
 
-    /*
-     * If someone calls snprintf(buf, 0, ...), then len == -1 here.
-     * Previously this code would assume an "unlimited" buffer size,
-     * thereby emulating sprintf().  Now we silently return and hope
-     * the caller doesn't expect us to terminate the buffer!
-     */
-    if (len < 0)
-       return;
     /*
      * First initialize the descriptor
      * Notice that if no length is given, we initialize buf_end to the
@@ -925,9 +919,8 @@ snprintf(char *buf, size_t len, const char *format,...)
 {
     int cc;
     va_list ap;
-    assert(len >= 0);
     va_start(ap, format);
-    strx_printv(&cc, buf, (len - 1), format, ap);
+    cc = vsnprintf(buf, len, format, ap);
     va_end(ap);
     return (cc);
 }
@@ -938,9 +931,16 @@ int
 vsnprintf(char *buf, size_t len, const char *format,
     va_list ap)
 {
-    int cc;
+    int cc = 0;
     assert(len >= 0);
-    strx_printv(&cc, buf, (len - 1), format, ap);
+    /*
+     * If someone calls snprintf(buf, 0, ...), then len becomes "(size_t)-1" in
+     * strx_printhv.  Previously this code would assume an "unlimited" buffer
+     * size, thereby emulating sprintf().  Now we silently do nothing and hope
+     * the caller doesn't expect us to terminate the buffer!
+     */
+    if (len > 0)
+       strx_printv(&cc, buf, (len - 1), format, ap);
     return (cc);
 }
 #endif