test_suite_debug: test the printf used by debug.c

In `test_suite_debug`, test `mbedtls_debug_snprintf()`, which uses
`mbedtls_vsnprintf()` like `mbedtls_debug_print_msg()`. Do this instead of
testing `mbedtls_snprintf()`, which might be subtly different (older
Windows runtimes had slightly different behavior for vsnprintf() vs
snprintf(); TF-PSA-Crypto might pick up a different function if the
platform configuration is different in TF-PSA-Crypto and Mbed TLS).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine
2026-02-17 17:08:12 +01:00
parent 7af09b4f21
commit 3c67824964
3 changed files with 26 additions and 3 deletions

View File

@@ -21,6 +21,16 @@
/* DEBUG_BUF_SIZE must be at least 2 */ /* DEBUG_BUF_SIZE must be at least 2 */
#define DEBUG_BUF_SIZE 512 #define DEBUG_BUF_SIZE 512
int mbedtls_debug_snprintf(char *dest, size_t maxlen,
const char *format, ...)
{
va_list argp;
va_start(argp, format);
int ret = mbedtls_vsnprintf(dest, maxlen, format, argp);
va_end(argp);
return ret;
}
static int debug_threshold = 0; static int debug_threshold = 0;
void mbedtls_debug_set_threshold(int threshold) void mbedtls_debug_set_threshold(int threshold)

View File

@@ -12,6 +12,19 @@
#include "mbedtls/debug.h" #include "mbedtls/debug.h"
/* This should be equivalent to mbedtls_snprintf(). But it might not be due
* to platform shenanigans. For example, Mbed TLS and TF-PSA-Crypto could
* have inconsistent platform definitions. On Mingw, some code might
* be built with a different setting of __USE_MINGW_ANSI_STDIO, resulting
* in an old non-C99 printf being used somewhere.
*
* Our library assumes that mbedtls_snprintf() and other printf functions
* are consistent throughout. This function is not an official API and
* is not meant to be used inside the library. It is provided to help
* debugging printf inconsistencies issues. If you need it, good luck!
*/
int mbedtls_debug_snprintf(char *dest, size_t maxlen,
const char *format, ...) MBEDTLS_PRINTF_ATTRIBUTE(3, 4);
/** /**
* \brief Print a message to the debug output. This function is always used * \brief Print a message to the debug output. This function is always used
* through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl * through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl

View File

@@ -116,11 +116,11 @@ void printf_int_expr(int format_indicator, intmax_t sizeof_x, intmax_t x, char *
/* Nominal case: buffer just large enough */ /* Nominal case: buffer just large enough */
TEST_CALLOC(output, n + 1); TEST_CALLOC(output, n + 1);
if ((size_t) sizeof_x <= sizeof(int)) { // Any smaller integers would be promoted to an int due to calling a vararg function if ((size_t) sizeof_x <= sizeof(int)) { // Any smaller integers would be promoted to an int due to calling a vararg function
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (int) x)); TEST_EQUAL(n, mbedtls_debug_snprintf(output, n + 1, format, (int) x));
} else if (sizeof_x == sizeof(long)) { } else if (sizeof_x == sizeof(long)) {
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long) x)); TEST_EQUAL(n, mbedtls_debug_snprintf(output, n + 1, format, (long) x));
} else if (sizeof_x == sizeof(long long)) { } else if (sizeof_x == sizeof(long long)) {
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, (long long) x)); TEST_EQUAL(n, mbedtls_debug_snprintf(output, n + 1, format, (long long) x));
} else { } else {
TEST_FAIL( TEST_FAIL(
"sizeof_x <= sizeof(int) || sizeof_x == sizeof(long) || sizeof_x == sizeof(long long)"); "sizeof_x <= sizeof(int) || sizeof_x == sizeof(long) || sizeof_x == sizeof(long long)");