From 83a4d648e35493d10b690929c567ec747e2fd85a Mon Sep 17 00:00:00 2001 From: Ramon Roche Date: Mon, 9 Feb 2026 15:09:52 -0800 Subject: [PATCH] logger: copy null terminator in write_info memcpy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit clang-tidy flags the memcpy of vlen bytes as bugprone-not-null-terminated-result because the destination buffer region is left unterminated in memory. Copy vlen + 1 bytes (including the source null terminator) so the buffer is null-terminated in memory. The ULog msg_size is not incremented for the extra byte — the null sits in the struct's key_value_str padding and is never written to the log file, preserving the ULog wire format which does not include a null terminator for string values. The bounds check (vlen < sizeof(msg) - msg_size) guarantees at least one byte of headroom beyond vlen, so vlen + 1 is always within the struct. Signed-off-by: Ramon Roche --- src/modules/logger/logger.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/logger/logger.cpp b/src/modules/logger/logger.cpp index 196550615e..7e65cf448c 100644 --- a/src/modules/logger/logger.cpp +++ b/src/modules/logger/logger.cpp @@ -1890,7 +1890,7 @@ void Logger::write_info(LogType type, const char *name, const char *value) /* copy string value directly to buffer */ if (vlen < (sizeof(msg) - msg_size)) { - memcpy(&buffer[msg_size], value, vlen); + memcpy(&buffer[msg_size], value, vlen + 1); msg_size += vlen; msg.msg_size = msg_size - ULOG_MSG_HEADER_LEN; @@ -1916,7 +1916,7 @@ void Logger::write_info_multiple(LogType type, const char *name, const char *val /* copy string value directly to buffer */ if (vlen < (sizeof(msg) - msg_size)) { - memcpy(&buffer[msg_size], value, vlen); + memcpy(&buffer[msg_size], value, vlen + 1); msg_size += vlen; msg.msg_size = msg_size - ULOG_MSG_HEADER_LEN;