Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: no newline when debug msg over DEBUG_BUF_SIZE #6511

Closed
wants to merge 8 commits into from
Closed
16 changes: 13 additions & 3 deletions library/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <stdio.h>
#include <string.h>

/* DEBUG_BUF_SIZE must be at least 2 */
#define DEBUG_BUF_SIZE 512

static int debug_threshold = 0;
Expand Down Expand Up @@ -69,6 +70,10 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's never going to trigger without #include <assert.h>.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bah, good spot - thanks

static_assert(DEBUG_BUF_SIZE >= 2)
#endif

if (NULL == ssl ||
NULL == ssl->conf ||
NULL == ssl->conf->f_dbg ||
Expand All @@ -80,10 +85,15 @@ 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';
if (ret < 0) {
ret = 0;
} else {
if (ret >= DEBUG_BUF_SIZE - 1) {
ret = DEBUG_BUF_SIZE - 2;
}
}
str[ret] = '\n';
str[ret + 1] = '\0';

debug_send_line(ssl, level, file, line, str);
}
Expand Down