Skip to content

Commit 906d3cd

Browse files
authored
Merge pull request #10020 from bensze01/msvc-format-size-macros
Fix preprocessor guards for C99 format size specifiers
2 parents 50432e4 + 24f11a3 commit 906d3cd

File tree

5 files changed

+109
-9
lines changed

5 files changed

+109
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Bugfix
2+
* Fix definition of MBEDTLS_PRINTF_SIZET to prevent runtime crashes that
3+
occurred whenever SSL debugging was enabled on a copy of Mbed TLS built
4+
with Visual Studio 2013 or MinGW.
5+
Fixes #10017.

include/mbedtls/debug.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,16 @@
108108
*
109109
* This module provides debugging functions.
110110
*/
111-
#if (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800)
111+
#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900)
112112
#include <inttypes.h>
113113
#define MBEDTLS_PRINTF_SIZET PRIuPTR
114114
#define MBEDTLS_PRINTF_LONGLONG "I64d"
115115
#else \
116-
/* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
116+
/* defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900) */
117117
#define MBEDTLS_PRINTF_SIZET "zu"
118118
#define MBEDTLS_PRINTF_LONGLONG "lld"
119119
#endif \
120-
/* (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
120+
/* defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900) */
121121

122122
#if !defined(MBEDTLS_PRINTF_MS_TIME)
123123
#include <inttypes.h>

tests/suites/test_suite_debug.data

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
printf "%" MBEDTLS_PRINTF_SIZET, 0
2+
printf_int_expr:PRINTF_SIZET:sizeof(size_t):0:"0"
3+
4+
printf "%" MBEDTLS_PRINTF_LONGLONG, 0
5+
printf_int_expr:PRINTF_LONGLONG:sizeof(long long):0:"0"
6+
7+
printf "%" MBEDTLS_PRINTF_MS_TIME, 0
8+
printf_int_expr:PRINTF_MS_TIME:sizeof(mbedtls_ms_time_t):0:"0"
9+
110
Debug print msg (threshold 1, level 0)
211
debug_print_msg_threshold:1:0:"MyFile":999:"MyFile(0999)\: Text message, 2 == 2\n"
312

tests/suites/test_suite_debug.function

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,34 @@
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+
12+
// Dummy type for builds without MBEDTLS_HAVE_TIME
13+
#if !defined(MBEDTLS_HAVE_TIME)
14+
typedef int64_t mbedtls_ms_time_t;
15+
#endif
16+
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+
729
struct buffer_data {
830
char buf[2000];
931
char *ptr;
1032
};
1133

34+
#if defined(MBEDTLS_SSL_TLS_C)
1235
static void string_debug(void *data, int level, const char *file, int line, const char *str)
1336
{
1437
struct buffer_data *buffer = (struct buffer_data *) data;
@@ -44,14 +67,77 @@ static void string_debug(void *data, int level, const char *file, int line, cons
4467

4568
buffer->ptr = p;
4669
}
70+
#endif /* MBEDTLS_SSL_TLS_C */
71+
72+
#if defined(_WIN32)
73+
static void noop_invalid_parameter_handler(
74+
const wchar_t *expression,
75+
const wchar_t *function,
76+
const wchar_t *file,
77+
unsigned int line,
78+
uintptr_t pReserved)
79+
{
80+
(void) expression;
81+
(void) function;
82+
(void) file;
83+
(void) line;
84+
(void) pReserved;
85+
}
86+
#endif /* _WIN32 */
87+
4788
/* END_HEADER */
4889

4990
/* BEGIN_DEPENDENCIES
50-
* depends_on:MBEDTLS_DEBUG_C:MBEDTLS_SSL_TLS_C
91+
* depends_on:MBEDTLS_DEBUG_C
5192
* END_DEPENDENCIES
5293
*/
5394

5495
/* BEGIN_CASE */
96+
void printf_int_expr(int format_indicator, intmax_t sizeof_x, intmax_t x, char *result)
97+
{
98+
#if defined(_WIN32)
99+
/* Windows treats any invalid format specifiers passsed to the CRT as fatal assertion failures.
100+
Disable this behaviour temporarily, so the rest of the test cases can complete. */
101+
_invalid_parameter_handler saved_handler =
102+
_set_invalid_parameter_handler(noop_invalid_parameter_handler);
103+
104+
// Disable assertion pop-up window in Debug builds
105+
int saved_report_mode = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE);
106+
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
107+
#endif
108+
109+
const char *format = printf_formats[format_indicator];
110+
char *output = NULL;
111+
const size_t n = strlen(result);
112+
113+
/* Nominal case: buffer just large enough */
114+
TEST_CALLOC(output, n + 1);
115+
if ((size_t) sizeof_x <= sizeof(int)) { // Any smaller integers would be promoted to an int due to calling a vararg function
116+
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (int) x));
117+
} else if (sizeof_x == sizeof(long)) {
118+
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long) x));
119+
} else if (sizeof_x == sizeof(long long)) {
120+
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long long) x));
121+
} else {
122+
TEST_FAIL(
123+
"sizeof_x <= sizeof(int) || sizeof_x == sizeof(long) || sizeof_x == sizeof(long long)");
124+
}
125+
TEST_MEMORY_COMPARE(result, n + 1, output, n + 1);
126+
127+
exit:
128+
mbedtls_free(output);
129+
output = NULL;
130+
131+
#if defined(_WIN32)
132+
// Restore default Windows behaviour
133+
_set_invalid_parameter_handler(saved_handler);
134+
_CrtSetReportMode(_CRT_ASSERT, saved_report_mode);
135+
(void) saved_report_mode;
136+
#endif
137+
}
138+
/* END_CASE */
139+
140+
/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
55141
void debug_print_msg_threshold(int threshold, int level, char *file,
56142
int line, char *result_str)
57143
{
@@ -89,7 +175,7 @@ exit:
89175
}
90176
/* END_CASE */
91177

92-
/* BEGIN_CASE */
178+
/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
93179
void mbedtls_debug_print_ret(char *file, int line, char *text, int value,
94180
char *result_str)
95181
{
@@ -124,7 +210,7 @@ exit:
124210
}
125211
/* END_CASE */
126212

127-
/* BEGIN_CASE */
213+
/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C */
128214
void mbedtls_debug_print_buf(char *file, int line, char *text,
129215
data_t *data, char *result_str)
130216
{
@@ -159,7 +245,7 @@ exit:
159245
}
160246
/* END_CASE */
161247

162-
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
248+
/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
163249
void mbedtls_debug_print_crt(char *crt_file, char *file, int line,
164250
char *prefix, char *result_str)
165251
{
@@ -199,7 +285,7 @@ exit:
199285
}
200286
/* END_CASE */
201287

202-
/* BEGIN_CASE depends_on:MBEDTLS_BIGNUM_C */
288+
/* BEGIN_CASE depends_on:MBEDTLS_SSL_TLS_C:MBEDTLS_BIGNUM_C */
203289
void mbedtls_debug_print_mpi(char *value, char *file, int line,
204290
char *prefix, char *result_str)
205291
{

0 commit comments

Comments
 (0)