From befe0fb3e142e287f79e0589c2e50996d9f10944 Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Tue, 8 Aug 2000 19:01:02 +0000 Subject: 2000-08-08 Jeff Johnston * libc/stdio/snprintf.c (snprintf, _snprintf_r): Fixed code so size of 0 results in nothing being written to string. Also fixed code so that when size is non-zero, there is only a maximum of size - 1 characters written to the array and a nul terminator is appended at the end. * libc/stdio/vsnprintf.c (vsnprintf, _vsnprintf_r): Ditto. --- newlib/libc/stdio/vsnprintf.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'newlib/libc/stdio/vsnprintf.c') diff --git a/newlib/libc/stdio/vsnprintf.c b/newlib/libc/stdio/vsnprintf.c index 18df5864a..5ca0ff27b 100644 --- a/newlib/libc/stdio/vsnprintf.c +++ b/newlib/libc/stdio/vsnprintf.c @@ -45,10 +45,11 @@ vsnprintf (str, size, fmt, ap) f._flags = __SWR | __SSTR; f._bf._base = f._p = (unsigned char *) str; - f._bf._size = f._w = size; + f._bf._size = f._w = (size > 0 ? size - 1 : 0); f._data = _REENT; ret = vfprintf (&f, fmt, ap); - *f._p = 0; + if (size > 0) + *f._p = 0; return ret; } @@ -65,9 +66,10 @@ vsnprintf_r (ptr, str, size, fmt, ap) f._flags = __SWR | __SSTR; f._bf._base = f._p = (unsigned char *) str; - f._bf._size = f._w = size; + f._bf._size = f._w = (size > 0 ? size - 1 : 0); f._data = ptr; ret = vfprintf (&f, fmt, ap); - *f._p = 0; + if (size > 0) + *f._p = 0; return ret; } -- cgit v1.2.3