From afbaac29e89ff5e538b76c925304ec2f581bf51a Mon Sep 17 00:00:00 2001 From: valord577 Date: Mon, 31 Oct 2022 15:17:37 +0800 Subject: [PATCH 1/8] Fix: no newline when debug msg over DEBUG_BUF_SIZE Signed-off-by: valord577 --- library/debug.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/debug.c b/library/debug.c index ab8b3524d4..cc88437f44 100644 --- a/library/debug.c +++ b/library/debug.c @@ -84,6 +84,10 @@ void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, str[ret] = '\n'; str[ret + 1] = '\0'; } + else + { + str[DEBUG_BUF_SIZE - 2] = '\n'; + } debug_send_line(ssl, level, file, line, str); } From 40fe1cd364daaef821f1a9150718c2c16f0dd03e Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Mon, 31 Oct 2022 11:11:27 +0000 Subject: [PATCH 2/8] Update library/debug.c Fix trailing white-space Signed-off-by: Dave Rodgman --- library/debug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/debug.c b/library/debug.c index cc88437f44..eecde10c03 100644 --- a/library/debug.c +++ b/library/debug.c @@ -84,7 +84,7 @@ void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, str[ret] = '\n'; str[ret + 1] = '\0'; } - else + else { str[DEBUG_BUF_SIZE - 2] = '\n'; } From e3623920cf0649fd0b26952ddd27446b5966a138 Mon Sep 17 00:00:00 2001 From: valord577 Date: Wed, 15 Feb 2023 19:01:16 +0800 Subject: [PATCH 3/8] send debug msg if contains '\n' Signed-off-by: valord577 --- library/debug.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/library/debug.c b/library/debug.c index eecde10c03..839ceba07c 100644 --- a/library/debug.c +++ b/library/debug.c @@ -68,6 +68,7 @@ void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, va_list argp; char str[DEBUG_BUF_SIZE]; int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + int newline = -1; if (NULL == ssl || NULL == ssl->conf || @@ -80,16 +81,26 @@ void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp); va_end(argp); - if (ret >= 0 && ret < DEBUG_BUF_SIZE - 1) { - str[ret] = '\n'; - str[ret + 1] = '\0'; - } - else - { - str[DEBUG_BUF_SIZE - 2] = '\n'; + if (DEBUG_BUF_SIZE >= 2) { + if (ret < 0) { + newline = 0; + } else { + newline = ret; + if (ret >= DEBUG_BUF_SIZE - 1) { + newline = DEBUG_BUF_SIZE - 2; + } + } } - debug_send_line(ssl, level, file, line, str); + /* + * Send if str contains '\n'. + */ + if (newline >= 0) { + str[newline] = '\n'; + str[newline + 1] = '\0'; + + debug_send_line(ssl, level, file, line, str); + } } void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level, From 06b0bb55e330a797e6edf9e4cd234ac1359724e7 Mon Sep 17 00:00:00 2001 From: valord577 Date: Wed, 15 Feb 2023 19:31:39 +0800 Subject: [PATCH 4/8] make code readable and change var name Signed-off-by: valord577 --- library/debug.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/library/debug.c b/library/debug.c index 839ceba07c..ae5e631d58 100644 --- a/library/debug.c +++ b/library/debug.c @@ -68,7 +68,11 @@ void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, va_list argp; char str[DEBUG_BUF_SIZE]; int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; - int newline = -1; + int eol = -1; + +#if defined(static_assert) + static_assert(DEBUG_BUF_SIZE >= 2) +#endif if (NULL == ssl || NULL == ssl->conf || @@ -81,23 +85,21 @@ void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp); va_end(argp); - if (DEBUG_BUF_SIZE >= 2) { - if (ret < 0) { - newline = 0; - } else { - newline = ret; - if (ret >= DEBUG_BUF_SIZE - 1) { - newline = DEBUG_BUF_SIZE - 2; - } + if (ret < 0) { + eol= 0; + } else { + eol= ret; + if (ret >= DEBUG_BUF_SIZE - 1) { + eol = DEBUG_BUF_SIZE - 2; } } /* * Send if str contains '\n'. */ - if (newline >= 0) { - str[newline] = '\n'; - str[newline + 1] = '\0'; + if (eol >= 0) { + str[eol] = '\n'; + str[eol + 1] = '\0'; debug_send_line(ssl, level, file, line, str); } From de6ea9847d7c323902fd42d038ccf050eeeaa710 Mon Sep 17 00:00:00 2001 From: valord577 Date: Wed, 15 Feb 2023 19:45:12 +0800 Subject: [PATCH 5/8] code style Signed-off-by: valord577 --- library/debug.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/debug.c b/library/debug.c index ae5e631d58..b405997bfd 100644 --- a/library/debug.c +++ b/library/debug.c @@ -86,9 +86,9 @@ void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, va_end(argp); if (ret < 0) { - eol= 0; + eol = 0; } else { - eol= ret; + eol = ret; if (ret >= DEBUG_BUF_SIZE - 1) { eol = DEBUG_BUF_SIZE - 2; } From 0d87d90cea256b10112f006b62064dd214d93cdf Mon Sep 17 00:00:00 2001 From: valord577 Date: Wed, 15 Feb 2023 21:46:47 +0800 Subject: [PATCH 6/8] simplify code Signed-off-by: valord577 --- library/debug.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/library/debug.c b/library/debug.c index b405997bfd..88c289b384 100644 --- a/library/debug.c +++ b/library/debug.c @@ -68,7 +68,6 @@ void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, va_list argp; char str[DEBUG_BUF_SIZE]; int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; - int eol = -1; #if defined(static_assert) static_assert(DEBUG_BUF_SIZE >= 2) @@ -86,23 +85,16 @@ void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, va_end(argp); if (ret < 0) { - eol = 0; + ret = 0; } else { - eol = ret; if (ret >= DEBUG_BUF_SIZE - 1) { - eol = DEBUG_BUF_SIZE - 2; + ret = DEBUG_BUF_SIZE - 2; } } + str[ret] = '\n'; + str[ret + 1] = '\0'; - /* - * Send if str contains '\n'. - */ - if (eol >= 0) { - str[eol] = '\n'; - str[eol + 1] = '\0'; - - debug_send_line(ssl, level, file, line, str); - } + debug_send_line(ssl, level, file, line, str); } void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level, From 001917898f48b4c7f8951085ed2f96c696615bae Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Wed, 15 Feb 2023 17:41:28 +0000 Subject: [PATCH 7/8] Document minimum size for DEBUG_BUF_SIZE Signed-off-by: Dave Rodgman --- library/debug.c | 1 + 1 file changed, 1 insertion(+) diff --git a/library/debug.c b/library/debug.c index 88c289b384..658d1c9d13 100644 --- a/library/debug.c +++ b/library/debug.c @@ -30,6 +30,7 @@ #include #include +/* DEBUG_BUF_SIZE must be at least 2 */ #define DEBUG_BUF_SIZE 512 static int debug_threshold = 0; From bd771820637ee26a3df6bf9933fa59803fdaf595 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Tue, 16 May 2023 16:43:48 +0100 Subject: [PATCH 8/8] Make use of MBEDTLS_STATIC_ASSERT Signed-off-by: Dave Rodgman --- library/debug.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/library/debug.c b/library/debug.c index 658d1c9d13..3e794b5565 100644 --- a/library/debug.c +++ b/library/debug.c @@ -70,9 +70,7 @@ void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, char str[DEBUG_BUF_SIZE]; int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; -#if defined(static_assert) - static_assert(DEBUG_BUF_SIZE >= 2) -#endif + MBEDTLS_STATIC_ASSERT(DEBUG_BUF_SIZE >= 2, "DEBUG_BUF_SIZE too small"); if (NULL == ssl || NULL == ssl->conf ||