From b10dd142734fe3b97931bdf4c60d4820cb23c461 Mon Sep 17 00:00:00 2001 From: Letter Date: Sat, 27 May 2023 08:51:58 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=AF=B9=20vsnprintf=20?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E7=9A=84=E9=94=99=E8=AF=AF=E7=94=A8=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/log/log.c | 7 ++++++- src/shell.c | 10 +++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/extensions/log/log.c b/extensions/log/log.c index 054e198..99afc86 100644 --- a/extensions/log/log.c +++ b/extensions/log/log.c @@ -189,7 +189,7 @@ static void logWriteBuffer(Log *log, LogLevel level, char *buffer, short len) void logWrite(Log *log, LogLevel level, const char *fmt, ...) { va_list vargs; - short len; + int len; #if LOG_USING_LOCK == 1 logLock(log); @@ -198,6 +198,11 @@ void logWrite(Log *log, LogLevel level, const char *fmt, ...) len = vsnprintf(logBuffer, LOG_BUFFER_SIZE - 1, fmt, vargs); va_end(vargs); + if (len > LOG_BUFFER_SIZE) + { + len = LOG_BUFFER_SIZE; + } + logWriteBuffer(log, level, logBuffer, len); #if LOG_USING_LOCK == 1 logUnlock(log); diff --git a/src/shell.c b/src/shell.c index a73349a..fde119c 100644 --- a/src/shell.c +++ b/src/shell.c @@ -377,14 +377,18 @@ void shellPrint(Shell *shell, const char *fmt, ...) { char buffer[SHELL_PRINT_BUFFER]; va_list vargs; + int len; SHELL_ASSERT(shell, return); va_start(vargs, fmt); - vsnprintf(buffer, SHELL_PRINT_BUFFER - 1, fmt, vargs); + len = vsnprintf(buffer, SHELL_PRINT_BUFFER, fmt, vargs); va_end(vargs); - - shellWriteString(shell, buffer); + if (len > SHELL_PRINT_BUFFER) + { + len = SHELL_PRINT_BUFFER; + } + shell->write(buffer, len); } #endif