From: rousskov <> Date: Fri, 28 Aug 1998 04:00:07 +0000 (+0000) Subject: - one IRIX an unsigned size_t creates problems when we do (len-1) in snprintf X-Git-Tag: SQUID_3_0_PRE1~2780 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ab8ec2d82f3b38117ade0bb2524df7b6bd3a379d;p=thirdparty%2Fsquid.git - one IRIX an unsigned size_t creates problems when we do (len-1) in snprintf fixed by cheking that len is positive before subtracting 1. - return value was sometimes uninitialized when calling snprintf --- diff --git a/lib/snprintf.c b/lib/snprintf.c index c3c31f3cff..d7edcbe1f0 100644 --- a/lib/snprintf.c +++ b/lib/snprintf.c @@ -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