libminc:vsnprintf add support for NULL destination pointer.

-Add support for returning the amount of characters that would have been
written if the buffer was large enough.
-Protect code against NULL dereference.

Change-Id: Ifb2041f4757e8a99f255d94768ba19621bc0ea16

http://gerrit.minix3.org/#/c/2560/
This commit is contained in:
Kees Jongenburger 2014-05-26 16:47:44 +02:00 committed by Lionel Sambuc
parent a1d08ebddc
commit 51b1cfd6ea

View file

@ -64,6 +64,11 @@ static void sputchar(int);
static void kdoprnt(void (*)(int), const char *, va_list);
static char *sbuf, *ebuf;
#if defined(__minix)
/* vsnprintf: add support for returning the amount of characters that would have been
* written if the buffer was large enough */
static int scount;
#endif /* defined(__minix) */
const char hexdigits[16] = "0123456789abcdef";
@ -134,7 +139,10 @@ do { \
static void
sputchar(int c)
{
#if defined(__minix)
scount++; /* increase scount regardless */
if (!sbuf) return; /* hanlde NULL sbuf */
#endif /* defined(__minix) */
if (sbuf < ebuf)
*sbuf++ = c;
}
@ -156,9 +164,21 @@ vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
sbuf = buf;
ebuf = buf + size - 1;
#if defined(__minix)
scount = 0; /* use scount to keep track of written items */
#endif /* defined(__minix) */
kdoprnt(sputchar, fmt, ap);
#if defined(__minix)
if (sbuf){ /* handle case where sbuf == NULL */
*sbuf = '\0';
}
return scount;
#else /* __minix is not defined */
*sbuf = '\0';
return sbuf - buf;
#endif /* defined(__minix) */
}
static void