From 353c17b5e7eaeb2d002311c9827682e3f6aa9f78 Mon Sep 17 00:00:00 2001 From: solar Date: Mon, 13 Jun 2011 13:40:45 +0000 Subject: [PATCH] Fixing #47 (*snprint() crash). git-svn-id: https://srv7.svn-repos.de/dev34/pdclib/trunk@519 546481bc-9713-0410-bf18-d3337bbf4a3e --- functions/stdio/snprintf.c | 2 ++ functions/stdio/vsnprintf.c | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/functions/stdio/snprintf.c b/functions/stdio/snprintf.c index 6dd3967..db6778c 100644 --- a/functions/stdio/snprintf.c +++ b/functions/stdio/snprintf.c @@ -35,6 +35,8 @@ int main( void ) { char target[100]; #include "printf_testcases.h" + TESTCASE( snprintf( NULL, 0, "foo" ) == 3 ); + TESTCASE( snprintf( NULL, 0, "%d", 100 ) == 3 ); return TEST_RESULTS; } diff --git a/functions/stdio/vsnprintf.c b/functions/stdio/vsnprintf.c index 9eb869e..06536d1 100644 --- a/functions/stdio/vsnprintf.c +++ b/functions/stdio/vsnprintf.c @@ -32,7 +32,12 @@ int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restric if ( ( *format != '%' ) || ( ( rc = _PDCLIB_print( format, &status ) ) == format ) ) { /* No conversion specifier, print verbatim */ - s[ status.i++ ] = *(format++); + if ( status.i < n ) + { + s[ status.i ] = *format; + } + status.i++; + format++; } else { @@ -40,7 +45,10 @@ int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restric format = rc; } } - s[ status.i ] = '\0'; + if ( status.i < n ) + { + s[ status.i ] = '\0'; + } va_end( status.arg ); return status.i; } -- 2.30.2