From 51b1cfd6ea50f569ca1370d37647dacd93eb9592 Mon Sep 17 00:00:00 2001 From: Kees Jongenburger Date: Mon, 26 May 2014 16:47:44 +0200 Subject: [PATCH] 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/ --- sys/lib/libsa/subr_prf.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/sys/lib/libsa/subr_prf.c b/sys/lib/libsa/subr_prf.c index 6a6c900f5..93f101728 100644 --- a/sys/lib/libsa/subr_prf.c +++ b/sys/lib/libsa/subr_prf.c @@ -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 -- 2.44.0