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 <bence.szepkuti@arm.com>
This commit is contained in:
Bence Szépkúti
2025-03-02 01:17:02 +01:00
parent 94b0eea23f
commit 5d554667c4

View File

@@ -2,6 +2,11 @@
#include "mbedtls/debug.h"
#include "string.h"
#if defined(_WIN32)
# include <stdlib.h>
# include <crtdbg.h>
#endif
struct buffer_data {
char buf[2000];
char *ptr;
@@ -44,6 +49,23 @@ static void string_debug(void *data, int level, const char *file, int line, cons
buffer->ptr = p;
}
#endif /* MBEDTLS_SSL_TLS_C */
#if defined(_WIN32)
static void noop_invalid_parameter_handler(
const wchar_t *expression,
const wchar_t *function,
const wchar_t *file,
unsigned int line,
uintptr_t pReserved)
{
(void) expression;
(void) function;
(void) file;
(void) line;
(void) pReserved;
}
#endif /* _WIN32 */
/* END_HEADER */
/* BEGIN_DEPENDENCIES
@@ -55,6 +77,17 @@ static void string_debug(void *data, int level, const char *file, int line, cons
void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework about string expressions */
intmax_t sizeof_x, intmax_t x, char *result)
{
#if defined(_WIN32)
/* Windows treats any invalid format specifiers passsed to the CRT as fatal assertion failures.
Disable this behaviour temporarily, so the rest of the test cases can complete. */
_invalid_parameter_handler saved_handler =
_set_invalid_parameter_handler(noop_invalid_parameter_handler);
// Disable assertion pop-up window in Debug builds
int saved_report_mode = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
#endif
const char *format = (char *) ((uintptr_t) smuggle_format_expr);
char *output = NULL;
const size_t n = strlen(result);
@@ -76,6 +109,13 @@ void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework
exit:
mbedtls_free(output);
output = NULL;
#if defined(_WIN32)
// Restore default Windows behaviour
_set_invalid_parameter_handler(saved_handler);
_CrtSetReportMode(_CRT_ASSERT, saved_report_mode);
(void) saved_report_mode;
#endif
}
/* END_CASE */