Skip to content

Commit f06b46d

Browse files
committed
Attempt to fix Windows build for tests.
Use sprintf_s (windows) or snprintf (others) instead of sprintf in unit tests.
1 parent 1c97e05 commit f06b46d

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

test/msgpack_c.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
#define msgpack_rand() drand48()
2222
#endif // _MSC_VER || __MINGW32__
2323

24+
#if defined(_MSC_VER)
25+
#define msgpack_snprintf sprintf_s
26+
#else // _MSC_VER
27+
#define msgpack_snprintf snprintf
28+
#endif // _MSC_VER
29+
2430
using namespace std;
2531

2632
const unsigned int kLoop = 10000;
@@ -1261,7 +1267,8 @@ TEST(MSGPACKC, simple_object_print_buffer_array_str)
12611267
EXPECT_EQ(str_size, o.via.str.size);
12621268
EXPECT_EQ(0, memcmp(str, o.via.str.ptr, str_size));
12631269

1264-
sprintf(expected, "[\"%s\"]", str);
1270+
msgpack_snprintf(expected, sizeof(expected), "[\"%s\"]", str);
1271+
expected[sizeof(expected) - 1] = '\0'; // not needed w/ sprintf_s
12651272
msgpack_object_print_buffer(buffer, sizeof(buffer) - 1, obj);
12661273
EXPECT_STREQ(expected, buffer);
12671274

@@ -1341,7 +1348,8 @@ TEST(MSGPACKC, simple_object_print_buffer_map_str)
13411348
EXPECT_EQ(mval_size, val.via.str.size);
13421349
EXPECT_EQ(0, memcmp(mval, val.via.str.ptr, mval_size));
13431350

1344-
sprintf(expected, "{\"%s\"=>\"%s\"}", mkey, mval);
1351+
msgpack_snprintf(expected, sizeof(expected), "{\"%s\"=>\"%s\"}", mkey, mval);
1352+
expected[sizeof(expected) - 1] = '\0'; // not needed w/ sprintf_s
13451353
msgpack_object_print_buffer(buffer, sizeof(buffer) - 1, obj);
13461354
EXPECT_STREQ(expected, buffer);
13471355

@@ -1384,7 +1392,8 @@ TEST(MSGPACKC, simple_object_print_buffer_map_str_empty)
13841392
EXPECT_EQ(MSGPACK_OBJECT_STR, val.type);
13851393
EXPECT_EQ(0UL, val.via.str.size);
13861394

1387-
sprintf(expected, "{\"%s\"=>\"\"}", mkey);
1395+
msgpack_snprintf(expected, sizeof(expected), "{\"%s\"=>\"\"}", mkey);
1396+
expected[sizeof(expected) - 1] = '\0'; // not needed w/ sprintf_s
13881397
msgpack_object_print_buffer(buffer, sizeof(buffer) - 1, obj);
13891398
EXPECT_STREQ(expected, buffer);
13901399

0 commit comments

Comments
 (0)