]> pd.if.org Git - pdclib/commitdiff
Fixing #47 (*snprint() crash).
authorsolar <unknown>
Mon, 13 Jun 2011 13:40:45 +0000 (13:40 +0000)
committersolar <unknown>
Mon, 13 Jun 2011 13:40:45 +0000 (13:40 +0000)
functions/stdio/snprintf.c
functions/stdio/vsnprintf.c

index 6dd39671fdf33647fb4b5d97f9022c3dad547372..db6778cc780fa6c635b1893763f846a03252923d 100644 (file)
@@ -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;
 }
 
index 9eb869e78cb2b7dbb52c9eab259b0fba1670283c..06536d177ff5e84c4765ff3c120caa7bc62a8242 100644 (file)
@@ -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;
 }