1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-12-14 02:22:15 +03:00

Use an array of strings instead of pointer smuggling

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
This commit is contained in:
Bence Szépkúti
2025-03-12 17:08:46 +01:00
parent cfadd96a9b
commit cb094f9192
2 changed files with 14 additions and 6 deletions

View File

@@ -1,9 +1,8 @@
# printf_int_expr expects a smuggled string expression as its first parameter
printf "%" MBEDTLS_PRINTF_SIZET, 0 printf "%" MBEDTLS_PRINTF_SIZET, 0
printf_int_expr:(uintptr_t) "%" MBEDTLS_PRINTF_SIZET:sizeof(size_t):0:"0" printf_int_expr:PRINTF_SIZET:sizeof(size_t):0:"0"
printf "%" MBEDTLS_PRINTF_LONGLONG, 0 printf "%" MBEDTLS_PRINTF_LONGLONG, 0
printf_int_expr:(uintptr_t) "%" MBEDTLS_PRINTF_LONGLONG:sizeof(long long):0:"0" printf_int_expr:PRINTF_LONGLONG:sizeof(long long):0:"0"
Debug print msg (threshold 1, level 0) Debug print msg (threshold 1, level 0)
debug_print_msg_threshold:1:0:"MyFile":999:"MyFile(0999)\: Text message, 2 == 2\n" debug_print_msg_threshold:1:0:"MyFile":999:"MyFile(0999)\: Text message, 2 == 2\n"

View File

@@ -7,6 +7,16 @@
# include <crtdbg.h> # include <crtdbg.h>
#endif #endif
typedef enum {
PRINTF_SIZET,
PRINTF_LONGLONG,
} printf_format_indicator_t;
const char *const printf_formats[] = {
[PRINTF_SIZET] = "%" MBEDTLS_PRINTF_SIZET,
[PRINTF_LONGLONG] = "%" MBEDTLS_PRINTF_LONGLONG,
};
struct buffer_data { struct buffer_data {
char buf[2000]; char buf[2000];
char *ptr; char *ptr;
@@ -74,8 +84,7 @@ static void noop_invalid_parameter_handler(
*/ */
/* BEGIN_CASE */ /* BEGIN_CASE */
void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework about string expressions */ void printf_int_expr(int format_indicator, intmax_t sizeof_x, intmax_t x, char *result)
intmax_t sizeof_x, intmax_t x, char *result)
{ {
#if defined(_WIN32) #if defined(_WIN32)
/* Windows treats any invalid format specifiers passsed to the CRT as fatal assertion failures. /* Windows treats any invalid format specifiers passsed to the CRT as fatal assertion failures.
@@ -88,7 +97,7 @@ void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
#endif #endif
const char *format = (char *) ((uintptr_t) smuggle_format_expr); const char *format = printf_formats[format_indicator];
char *output = NULL; char *output = NULL;
const size_t n = strlen(result); const size_t n = strlen(result);