From 36099367c88765bbf851e75002504116adadd824 Mon Sep 17 00:00:00 2001 From: Bryan Biedenkapp Date: Fri, 31 May 2024 10:56:08 -0400 Subject: [PATCH] fix condition where the Log and ActivityLog would overflow the character buffer by no properly calculating the length of the va_args string (this should resolve buffer overflow terminations on GCC 13+); --- src/common/Log.cpp | 9 ++++++--- src/fne/ActivityLog.cpp | 7 +++++-- src/host/ActivityLog.cpp | 7 +++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/common/Log.cpp b/src/common/Log.cpp index ec9d21dc..3610865d 100644 --- a/src/common/Log.cpp +++ b/src/common/Log.cpp @@ -35,7 +35,7 @@ #define EOL "\r\n" -const uint32_t LOG_BUFFER_LEN = 16384U; +const uint32_t LOG_BUFFER_LEN = 4096U; // --------------------------------------------------------------------------- // Global Variables @@ -216,11 +216,14 @@ void Log(uint32_t level, const char *module, const char* fmt, ...) } } - va_list vl; + va_list vl, vl_len; va_start(vl, fmt); + va_copy(vl_len, vl); - ::vsnprintf(buffer + ::strlen(buffer), LOG_BUFFER_LEN - 1U, fmt, vl); + size_t len = ::vsnprintf(nullptr, 0U, fmt, vl_len); + ::vsnprintf(buffer + ::strlen(buffer), len + 1U, fmt, vl); + va_end(vl_len); va_end(vl); if (m_outStream && g_logDisplayLevel == 0U) { diff --git a/src/fne/ActivityLog.cpp b/src/fne/ActivityLog.cpp index a25d7e1f..1d923c5f 100644 --- a/src/fne/ActivityLog.cpp +++ b/src/fne/ActivityLog.cpp @@ -119,11 +119,14 @@ void ActivityLog(const char* msg, ...) char buffer[ACT_LOG_BUFFER_LEN]; - va_list vl; + va_list vl, vl_len; va_start(vl, msg); + va_copy(vl_len, vl); - ::vsnprintf(buffer, ACT_LOG_BUFFER_LEN - 1U, msg, vl); + size_t len = ::vsnprintf(nullptr, 0U, msg, vl_len); + ::vsnprintf(buffer, len + 1U, msg, vl); + va_end(vl_len); va_end(vl); bool ret = ::ActivityLogOpen(); diff --git a/src/host/ActivityLog.cpp b/src/host/ActivityLog.cpp index 2fc1899a..544202f1 100644 --- a/src/host/ActivityLog.cpp +++ b/src/host/ActivityLog.cpp @@ -138,11 +138,14 @@ void ActivityLog(const char *mode, const bool sourceRf, const char* msg, ...) ::sprintf(buffer, "A: %04d-%02d-%02d %02d:%02d:%02d.%03lu %s %s ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec / 1000U, mode, (sourceRf) ? "RF" : "Net"); } - va_list vl; + va_list vl, vl_len; va_start(vl, msg); + va_copy(vl_len, vl); - ::vsnprintf(buffer + ::strlen(buffer), ACT_LOG_BUFFER_LEN - 1U, msg, vl); + size_t len = ::vsnprintf(nullptr, 0U, msg, vl_len); + ::vsnprintf(buffer + ::strlen(buffer), len + 1U, msg, vl); + va_end(vl_len); va_end(vl); bool ret = ::ActivityLogOpen();