/*
* 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,
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
{
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);
}
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