From 25418ac73475c350f94ea8adf48da0c48fcfced7 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 3969616f4..a0cad4365 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 9ecf5f96df4aec225bac216fd77674bf8cdbf71d 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 a0cad4365..5c8d573dc 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 24da0cd0f93a6b9e6bad8f4060eb6e7eb0ab7096 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 5c8d573dc..09a1d9efd 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 536893c22fb446b220fb11bd4f6b8ffc79377467 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 09a1d9efd..b146e76c0 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 176e92711c46d2f44ba222096790474b421ae233 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 b146e76c0..0e9854857 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 5bfcd1c63b2eb23548db502c95e832c3c447c69d 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 0e9854857..ad5657314 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 ed59ea76a6a8b4732d8f0dfd382afd512c41600d 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 ad5657314..8a5b28758 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 8508e50d3dbb61014a58e594ca46ee3ffb4c42a0 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 8a5b28758..e3dfaefb0 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 ||