Skip to content

Commit d2933f0

Browse files
committed
Use an array of strings instead of pointer smuggling
Signed-off-by: Bence Szépkúti <[email protected]>
1 parent 46e0b1c commit d2933f0

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

tests/suites/test_suite_debug.data

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# printf_int_expr expects a smuggled string expression as its first parameter
22
printf "%" MBEDTLS_PRINTF_SIZET, 0
3-
printf_int_expr:(uintptr_t) "%" MBEDTLS_PRINTF_SIZET:sizeof(size_t):0:"0"
3+
printf_int_expr:PRINTF_SIZET:sizeof(size_t):0:"0"
44

55
printf "%" MBEDTLS_PRINTF_LONGLONG, 0
6-
printf_int_expr:(uintptr_t) "%" MBEDTLS_PRINTF_LONGLONG:sizeof(long long):0:"0"
6+
printf_int_expr:PRINTF_LONGLONG:sizeof(long long):0:"0"
77

88
printf "%" MBEDTLS_PRINTF_MS_TIME, 0
9-
printf_int_expr:(uintptr_t) "%" MBEDTLS_PRINTF_MS_TIME:sizeof(mbedtls_ms_time_t):0:"0"
9+
printf_int_expr:PRINTF_MS_TIME:sizeof(mbedtls_ms_time_t):0:"0"
1010

1111
Debug print msg (threshold 1, level 0)
1212
debug_print_msg_threshold:1:0:"MyFile":999:"MyFile(0999)\: Text message, 2 == 2\n"

tests/suites/test_suite_debug.function

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
typedef int64_t mbedtls_ms_time_t;
1515
#endif
1616

17+
typedef enum {
18+
PRINTF_SIZET,
19+
PRINTF_LONGLONG,
20+
PRINTF_MS_TIME,
21+
} printf_format_indicator_t;
22+
23+
const char const* printf_formats[] = {
24+
[PRINTF_SIZET] = "%" MBEDTLS_PRINTF_SIZET,
25+
[PRINTF_LONGLONG] = "%" MBEDTLS_PRINTF_LONGLONG,
26+
[PRINTF_MS_TIME] = "%" MBEDTLS_PRINTF_MS_TIME,
27+
};
28+
1729
struct buffer_data {
1830
char buf[2000];
1931
char *ptr;
@@ -81,8 +93,7 @@ static void noop_invalid_parameter_handler(
8193
*/
8294

8395
/* BEGIN_CASE */
84-
void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework about string expressions */
85-
intmax_t sizeof_x, intmax_t x, char *result)
96+
void printf_int_expr(int format_indicator, intmax_t sizeof_x, intmax_t x, char *result)
8697
{
8798
#if defined(_WIN32)
8899
/* Windows treats any invalid format specifiers passsed to the CRT as fatal assertion failures.
@@ -95,7 +106,7 @@ void printf_int_expr(intmax_t smuggle_format_expr, /* TODO: teach test framework
95106
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
96107
#endif
97108

98-
const char *format = (char *) ((uintptr_t) smuggle_format_expr);
109+
const char *format = printf_formats[format_indicator];
99110
char *output = NULL;
100111
const size_t n = strlen(result);
101112

0 commit comments

Comments
 (0)