From 4f3a21f40d17ff7b24b27a121023f04aeab9ceb3 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 20 Feb 2026 17:47:55 +0100 Subject: [PATCH] Stop using MBEDTLS_PRINTF_SIZET Since Mbed TLS 3.6.0, all officially supported versions of Visual Studio a printf function family that is sufficiently compliant to C99 for our purposes, in particular supporting `%zu` for `size_t`. The only platform without `%zu` that we semi-officially support is older versions of MinGW, still used in our CI. MinGW provides either a Windows legacy printf or a standards-compliant printf depending on the value of `__USE_MINGW_ANSI_STDIO` when compiling each C file. Force the use of the compliant version. Don't rely on `MBEDTLS_PRINTF_SIZET`, which is defined in `` and no longer considers the Windows legacy version in Mbed TLS >= 4.1. Signed-off-by: Gilles Peskine --- psasim/src/aut_psa_key_agreement.c | 6 ++--- .../mbedtls_framework/c_wrapper_generator.py | 14 ++++++++++-- tests/programs/metatest.c | 22 ++++++++++--------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/psasim/src/aut_psa_key_agreement.c b/psasim/src/aut_psa_key_agreement.c index 4a0aab147..4ab1bf46f 100644 --- a/psasim/src/aut_psa_key_agreement.c +++ b/psasim/src/aut_psa_key_agreement.c @@ -69,7 +69,7 @@ int psa_key_agreement_main(void) return EXIT_FAILURE; } - mbedtls_printf("Client Public Key (%" MBEDTLS_PRINTF_SIZET " bytes):\n", client_pk_len); + mbedtls_printf("Client Public Key (%zu bytes):\n", client_pk_len); for (size_t j = 0; j < client_pk_len; j++) { if (j % 8 == 0) { @@ -108,7 +108,7 @@ int psa_key_agreement_main(void) return EXIT_FAILURE; } - mbedtls_printf("Server Public Key (%" MBEDTLS_PRINTF_SIZET " bytes):\n", sizeof(server_pk)); + mbedtls_printf("Server Public Key (%zu bytes):\n", sizeof(server_pk)); for (size_t j = 0; j < sizeof(server_pk); j++) { if (j % 8 == 0) { @@ -129,7 +129,7 @@ int psa_key_agreement_main(void) return EXIT_FAILURE; } - mbedtls_printf("Derived Key (%" MBEDTLS_PRINTF_SIZET " bytes):\n", derived_key_len); + mbedtls_printf("Derived Key (%zu bytes):\n", derived_key_len); for (size_t j = 0; j < derived_key_len; j++) { if (j % 8 == 0) { diff --git a/scripts/mbedtls_framework/c_wrapper_generator.py b/scripts/mbedtls_framework/c_wrapper_generator.py index ea52d3abd..bc777033c 100644 --- a/scripts/mbedtls_framework/c_wrapper_generator.py +++ b/scripts/mbedtls_framework/c_wrapper_generator.py @@ -110,6 +110,17 @@ class Base: ''') + if not header: + # On Mingw-w64, force the use of a C99-compliant printf() and friends. + # This is necessary on older versions of Mingw and/or Windows runtimes + # where snprintf does not always zero-terminate the buffer, and does + # not support formats such as "%zu" for size_t and "%lld" for long long. + prologue += strip_indentation(f''' + #if !defined(__USE_MINGW_ANSI_STDIO) + #define __USE_MINGW_ANSI_STDIO 1 + #endif + ''') + for include in self._INCLUDES: prologue += "#include {}\n".format(include) @@ -403,7 +414,6 @@ class Logging(Base): #if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_TEST_HOOKS) #include #include -#include // for MBEDTLS_PRINTF_SIZET #include // for mbedtls_fprintf #endif /* defined(MBEDTLS_FS_IO) && defined(MBEDTLS_TEST_HOOKS) */ """) @@ -412,7 +422,7 @@ class Logging(Base): 'int': '%d', 'long': '%ld', 'long long': '%lld', - 'size_t': '%"MBEDTLS_PRINTF_SIZET"', + 'size_t': '%zu', 'unsigned': '0x%08x', 'unsigned int': '0x%08x', 'unsigned long': '0x%08lx', diff --git a/tests/programs/metatest.c b/tests/programs/metatest.c index b862889a1..4345061e9 100644 --- a/tests/programs/metatest.c +++ b/tests/programs/metatest.c @@ -26,6 +26,14 @@ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ +/* On Mingw-w64, force the use of a C99-compliant printf() and friends. + * This is necessary on older versions of Mingw and/or Windows runtimes + * where snprintf does not always zero-terminate the buffer, and does + * not support formats such as "%zu" for size_t and "%lld" for long long. + */ +#if !defined(__USE_MINGW_ANSI_STDIO) +#define __USE_MINGW_ANSI_STDIO 1 +#endif #include #include @@ -200,10 +208,7 @@ static void test_memory_poison(const char *name) size_t start = 0, offset = 0, count = 0; char direction = 'r'; if (sscanf(name, - "%*[^0-9]%" MBEDTLS_PRINTF_SIZET - "%*[^0-9]%" MBEDTLS_PRINTF_SIZET - "%*[^0-9]%" MBEDTLS_PRINTF_SIZET - "_%c", + "%*[^0-9]%zu%*[^0-9]%zu%*[^0-9]%zu_%c", &start, &offset, &count, &direction) != 4) { mbedtls_fprintf(stderr, "%s: Bad name format: %s\n", __func__, name); return; @@ -217,22 +222,19 @@ static void test_memory_poison(const char *name) if (start > sizeof(aligned.buf)) { mbedtls_fprintf(stderr, - "%s: start=%" MBEDTLS_PRINTF_SIZET - " > size=%" MBEDTLS_PRINTF_SIZET, + "%s: start=%zu > size=%zu", __func__, start, sizeof(aligned.buf)); return; } if (start + count > sizeof(aligned.buf)) { mbedtls_fprintf(stderr, - "%s: start+count=%" MBEDTLS_PRINTF_SIZET - " > size=%" MBEDTLS_PRINTF_SIZET, + "%s: start+count=%zu > size=%zu", __func__, start + count, sizeof(aligned.buf)); return; } if (offset >= count) { mbedtls_fprintf(stderr, - "%s: offset=%" MBEDTLS_PRINTF_SIZET - " >= count=%" MBEDTLS_PRINTF_SIZET, + "%s: offset=%zu >= count=%zu", __func__, offset, count); return; }