From 557d0a24ff973c73866df9cd494fca0f13d1d02c Mon Sep 17 00:00:00 2001 From: Deomid rojer Ryabkov Date: Tue, 19 Mar 2024 00:55:43 +0000 Subject: [PATCH] mbedtls_debug_print_crt: Reduce stack usage Allocate a temporary debug buffer on the heap Signed-off-by: Deomid rojer Ryabkov --- library/debug.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/library/debug.c b/library/debug.c index c36ed3c5c2..598da2ee80 100644 --- a/library/debug.c +++ b/library/debug.c @@ -385,7 +385,7 @@ void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level, const char *file, int line, const char *text, const mbedtls_x509_crt *crt) { - char str[DEBUG_BUF_SIZE]; + char *buf = NULL; int i = 0; if (NULL == ssl || @@ -396,19 +396,22 @@ void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level, return; } + buf = mbedtls_calloc(1, 1024); + if (buf == NULL) { + return; + } while (crt != NULL) { - char buf[1024]; + mbedtls_snprintf(buf, 1023, "%s #%d:\n", text, ++i); + debug_send_line(ssl, level, file, line, buf); - mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i); - debug_send_line(ssl, level, file, line, str); - - mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt); + mbedtls_x509_crt_info(buf, 1023, "", crt); debug_print_line_by_line(ssl, level, file, line, buf); debug_print_pk(ssl, level, file, line, "crt->", &crt->pk); crt = crt->next; } + mbedtls_free(buf); } #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */