Skip to content

Commit d80553d

Browse files
committed
Disable fatal assertions in Windows printf tests
The Windows CRT treats any invalid format specifiers passed to the CRT as fatal assertion failures. Disable thie behaviour temporarily while testing if the format specifiers we use are supported. Signed-off-by: Bence Szépkúti <[email protected]>
1 parent 1554f6b commit d80553d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/suites/test_suite_debug.function

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
#include "mbedtls/pk.h"
55
#include <test/ssl_helpers.h>
66

7+
#if defined(_WIN32)
8+
# include <stdlib.h>
9+
# include <crtdbg.h>
10+
#endif
11+
712
// Use a macro instead of sizeof(mbedtls_ms_time_t) because the expression store
813
// doesn't exclude entries based on depends_on headers, which would cause failures
914
// in builds without MBEDTLS_HAVE_TIME
@@ -55,6 +60,23 @@ static void string_debug(void *data, int level, const char *file, int line, cons
5560
buffer->ptr = p;
5661
}
5762
#endif /* MBEDTLS_SSL_TLS_C */
63+
64+
#if defined(_WIN32)
65+
static void noop_invalid_parameter_handler(
66+
const wchar_t *expression,
67+
const wchar_t *function,
68+
const wchar_t *file,
69+
unsigned int line,
70+
uintptr_t pReserved)
71+
{
72+
(void) expression;
73+
(void) function;
74+
(void) file;
75+
(void) line;
76+
(void) pReserved;
77+
}
78+
#endif /* _WIN32 */
79+
5880
/* END_HEADER */
5981

6082
/* BEGIN_DEPENDENCIES
@@ -66,6 +88,17 @@ static void string_debug(void *data, int level, const char *file, int line, cons
6688
void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework about string expressions */
6789
intmax_t sizeof_x, intmax_t x, char *result)
6890
{
91+
#if defined(_WIN32)
92+
/* Windows treats any invalid format specifiers passsed to the CRT as fatal assertion failures.
93+
Disable this behaviour temporarily, so the rest of the test cases can complete. */
94+
_invalid_parameter_handler saved_handler =
95+
_set_invalid_parameter_handler(noop_invalid_parameter_handler);
96+
97+
// Disable assertion pop-up window in Debug builds
98+
int saved_report_mode = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE);
99+
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
100+
#endif
101+
69102
const char *format = (char *) ((uintptr_t) smuggle_format_expr);
70103
char *output = NULL;
71104
const size_t n = strlen(result);
@@ -87,6 +120,13 @@ void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework
87120
exit:
88121
mbedtls_free(output);
89122
output = NULL;
123+
124+
#if defined(_WIN32)
125+
// Restore default Windows behaviour
126+
_set_invalid_parameter_handler(saved_handler);
127+
_CrtSetReportMode(_CRT_ASSERT, saved_report_mode);
128+
(void) saved_report_mode;
129+
#endif
90130
}
91131
/* END_CASE */
92132

0 commit comments

Comments
 (0)