diff options
author | tomhudson@google.com <tomhudson@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-07-08 17:49:22 +0000 |
---|---|---|
committer | tomhudson@google.com <tomhudson@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-07-08 17:49:22 +0000 |
commit | 47e0a09052eb4c93a4506ebf3c64070fafc3d4b3 (patch) | |
tree | e9f7be606c243a90b9a6a5df49cdc6cd851e60fa | |
parent | cd9cfd7de39f8e7696cdb6b72931294d9da7839f (diff) |
Replace vsnprintf with vsnprintf_s; add code to reproduce issue to StringTest;
fix another Windows test failure.
git-svn-id: http://skia.googlecode.com/svn/trunk@1829 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | src/core/SkString.cpp | 4 | ||||
-rw-r--r-- | tests/StringTest.cpp | 42 |
2 files changed, 45 insertions, 1 deletions
diff --git a/src/core/SkString.cpp b/src/core/SkString.cpp index 0d7defec8a..c8e570bee1 100644 --- a/src/core/SkString.cpp +++ b/src/core/SkString.cpp @@ -25,7 +25,8 @@ static const size_t kBufferSize = 256; #ifdef SK_BUILD_FOR_WIN - #define VSNPRINTF _vsnprintf + #define VSNPRINTF(buffer, size, format, args) \ + _vsnprintf_s(buffer, size, _TRUNCATE, format, args) #define SNPRINTF _snprintf #else #define VSNPRINTF vsnprintf @@ -615,4 +616,5 @@ SkString SkStringPrintf(const char* format, ...) { } #undef VSNPRINTF +#undef SNPRINTF diff --git a/tests/StringTest.cpp b/tests/StringTest.cpp index 02510d56dc..d8814224c0 100644 --- a/tests/StringTest.cpp +++ b/tests/StringTest.cpp @@ -1,5 +1,31 @@ #include "Test.h" #include "SkString.h" +#include <stdarg.h> + + +// Windows vsnprintf doesn't 0-terminate safely), but is so far +// encapsulated in SkString that we can't test it directly. + +#ifdef SK_BUILD_FOR_WIN + #define VSNPRINTF(buffer, size, format, args) \ + vsnprintf_s(buffer, size, _TRUNCATE, format, args) +#else + #define VSNPRINTF vsnprintf +#endif + +#define ARGS_TO_BUFFER(format, buffer, size) \ + do { \ + va_list args; \ + va_start(args, format); \ + VSNPRINTF(buffer, size, format, args); \ + va_end(args); \ + } while (0) + +void printfAnalog(char* buffer, int size, const char format[], ...) { + ARGS_TO_BUFFER(format, buffer, size); +} + + static void TestString(skiatest::Reporter* reporter) { SkString a; @@ -74,8 +100,13 @@ static void TestString(skiatest::Reporter* reporter) { { -SK_Scalar1, "-1" }, { SK_Scalar1/2, "0.5" }, #ifdef SK_SCALAR_IS_FLOAT + #ifdef SK_BUILD_FOR_WIN + { 3.4028234e38f, "3.4028235e+038" }, + { -3.4028234e38f, "-3.4028235e+038" }, + #else { 3.4028234e38f, "3.4028235e+38" }, { -3.4028234e38f, "-3.4028235e+38" }, + #endif #endif }; for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { @@ -87,6 +118,17 @@ static void TestString(skiatest::Reporter* reporter) { } REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0")); + + char buffer [40]; + memset(buffer, 'a', 40); + REPORTER_ASSERT(reporter, buffer[18] == 'a'); + REPORTER_ASSERT(reporter, buffer[19] == 'a'); + REPORTER_ASSERT(reporter, buffer[20] == 'a'); + printfAnalog(buffer, 20, "%30d", 0); + REPORTER_ASSERT(reporter, buffer[18] == ' '); + REPORTER_ASSERT(reporter, buffer[19] == 0); + REPORTER_ASSERT(reporter, buffer[20] == 'a'); + } #include "TestClassDef.h" |