Skip to content

Commit 870a3e9

Browse files
hidmicahcorde
authored andcommitted
Fix vsnprintf mocks for Release builds. (#274)
Signed-off-by: Michel Hidalgo <[email protected]>
1 parent ecad08b commit 870a3e9

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

test/mocking_utils/stdio.hpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,26 @@ using _vsnprintf_s_type =
5252
/// Patch _vscprintf with the given `replacement` in the given `scope`.
5353
#define patch__vscprintf(scope, replacement) patch(scope, _vscprintf, replacement)
5454

55-
#endif // defined(_WIN32)
55+
#else // defined(_WIN32)
56+
57+
// Deal with binary API nuances
58+
#ifndef NDEBUG
59+
60+
/// Patch vsnprintf with the given `replacement` in the given `scope`.
61+
#define patch_vsnprintf(scope, replacement) patch(scope, vsnprintf, replacement)
62+
63+
#else
64+
65+
/// Patch vsnprintf with the given `replacement` in the given `scope`.
66+
#define patch_vsnprintf(scope, replacement) patch( \
67+
scope, __vsnprintf_chk, [&](char * __str, size_t, int, size_t __size, \
68+
const char * __fmt, va_list __ap) { \
69+
auto f = replacement; \
70+
return f(__str, __size, __fmt, __ap); \
71+
})
72+
73+
#endif
74+
#endif
5675

5776
} // namespace mocking_utils
5877

test/test_char_array.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ TEST_F(ArrayCharTest, vsprintf_fail) {
144144
return -1;
145145
});
146146
#else
147-
auto mock = mocking_utils::patch(
148-
"lib:rcutils", vsnprintf, [&](char * buffer, size_t buffer_size, auto...) {
147+
auto mock = mocking_utils::patch_vsnprintf(
148+
"lib:rcutils", [&](char * buffer, size_t buffer_size, auto...) {
149149
if (nullptr == buffer || buffer_size < buffer_threshold) {
150150
return static_cast<int>(buffer_threshold);
151151
}

test/test_format_string.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ TEST(test_format_string_limit, on_internal_error) {
7575
return -1;
7676
});
7777
#else
78-
auto mock = mocking_utils::patch(
79-
"lib:rcutils", vsnprintf, [](char * buffer, auto && ...) {
78+
auto mock = mocking_utils::patch_vsnprintf(
79+
"lib:rcutils", [](char * buffer, auto && ...) {
8080
if (nullptr == buffer) {
8181
return 1; // provide a dummy value if buffer required size is queried
8282
}

test/test_shared_library.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ TEST_F(TestSharedLibrary, basic_load) {
6161
return -1;
6262
});
6363
#else
64-
auto mock = mocking_utils::patch(
65-
"lib:rcutils", vsnprintf, [](char * buffer, auto && ...) {
64+
auto mock = mocking_utils::patch_vsnprintf(
65+
"lib:rcutils", [](char * buffer, auto && ...) {
6666
if (nullptr == buffer) {
6767
return 1; // provide a dummy value if buffer required size is queried
6868
}

test/test_time.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ TEST_F(TestTimeFixture, test_rcutils_time_point_value_as_nanoseconds_string) {
311311
return -1;
312312
});
313313
#else
314-
auto mock = mocking_utils::patch(
315-
"lib:rcutils", vsnprintf, [](char * buffer, auto && ...) {
314+
auto mock = mocking_utils::patch_vsnprintf(
315+
"lib:rcutils", [](char * buffer, auto && ...) {
316316
if (nullptr == buffer) {
317317
return 1; // provide a dummy value if buffer required size is queried
318318
}
@@ -391,8 +391,8 @@ TEST_F(TestTimeFixture, test_rcutils_time_point_value_as_seconds_string) {
391391
return -1;
392392
});
393393
#else
394-
auto mock = mocking_utils::patch(
395-
"lib:rcutils", vsnprintf, [](char * buffer, auto && ...) {
394+
auto mock = mocking_utils::patch_vsnprintf(
395+
"lib:rcutils", [](char * buffer, auto && ...) {
396396
if (nullptr == buffer) {
397397
return 1; // provide a dummy value if buffer required size is queried
398398
}

0 commit comments

Comments
 (0)