changeset 3200:5e8c9e64b5b0

* src/common/vsnprintf.c: If string truncated (C99 case), apply the null termination in correct place. * tests/sprintf2.c: Fix checks for return values if C99 implementation snprintf() used.
author sergeig
date Tue, 19 Feb 2013 13:37:12 +0000
parents 1151035b20da
children f5d2adaa8595
files packages/language/c/libc/stdio/current/ChangeLog packages/language/c/libc/stdio/current/src/common/vsnprintf.cxx packages/language/c/libc/stdio/current/tests/sprintf2.c
diffstat 3 files changed, 26 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/packages/language/c/libc/stdio/current/ChangeLog
+++ b/packages/language/c/libc/stdio/current/ChangeLog
@@ -1,3 +1,10 @@
+2012-02-19  Sergei Gavrikov  <sergei.gavrikov@gmail.com>
+
+	* src/common/vsnprintf.c: If string truncated (C99 case), apply the
+	null termination in correct place.
+	* tests/sprintf2.c: Fix checks for return values if C99 implementation
+	snprintf() used.
+
 2009-08-20  Uwe Kindler  <uwe_kindler@web.de>
 
 	* cdl/stdio.cdl Added option CYGIMP_LIBC_STDIO_C99_SNPRINTF
--- a/packages/language/c/libc/stdio/current/src/common/vsnprintf.cxx
+++ b/packages/language/c/libc/stdio/current/src/common/vsnprintf.cxx
@@ -113,6 +113,13 @@ vsnprintf( char *s, size_t size, const c
      */
     if ( (ret >= 0) && ((size_t)ret < size) )
         s[ret] = '\0';
+#ifdef CYGIMP_LIBC_STDIO_C99_SNPRINTF
+    /* C99 case. If no error, and string truncated, apply null termination
+     * in correct place
+     */
+    if ( (ret >= 0) && ((size_t)ret >= size) && (size > 0) )
+        s[size-1] = '\0';
+#endif
     return ret;
 } // vsnprintf()
 
--- a/packages/language/c/libc/stdio/current/tests/sprintf2.c
+++ b/packages/language/c/libc/stdio/current/tests/sprintf2.c
@@ -136,14 +136,22 @@ test( CYG_ADDRWORD data )
     ret = snprintf(y, 19, "print up to here >< and not this bit" );
     my_strcpy(z, "print up to here >");
     CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "simple snprintf test #1");
+#ifdef CYGIMP_LIBC_STDIO_C99_SNPRINTF
+    CYG_TEST_PASS_FAIL(ret > my_strlen(z),
+#else
     CYG_TEST_PASS_FAIL(ret == my_strlen(z),
+#endif
                        "simple snprintf test #1 return code" );
     
     // Check 4
     ret = snprintf(y, 31, "print a bit of this number: %05d nyer", 1234);
     my_strcpy(z, "print a bit of this number: 01");
     CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "simple snprintf test #2");
+#ifdef CYGIMP_LIBC_STDIO_C99_SNPRINTF
+    CYG_TEST_PASS_FAIL(ret > my_strlen(z),
+#else
     CYG_TEST_PASS_FAIL(ret == my_strlen(z),
+#endif
                        "simple snprintf test #2 return code" );
     
 #ifdef CYGSEM_LIBC_STDIO_PRINTF_FLOATING_POINT
@@ -168,7 +176,11 @@ test( CYG_ADDRWORD data )
     CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0,
                        "snprintf double test #1");
 
+#ifdef CYGIMP_LIBC_STDIO_C99_SNPRINTF
+    CYG_TEST_PASS_FAIL(ret > my_strlen(z),
+#else
     CYG_TEST_PASS_FAIL(ret == my_strlen(z),
+#endif
                        "snprintf double test #1 return code");
 
 #endif // ifdef CYGSEM_LIBC_STDIO_PRINTF_FLOATING_POINT