Skip to content

Commit 07a0117

Browse files
fix(log_router): stop buffer overrun
The return value of vsnprintf() is used to determine how long the message was. However, when vsprintf() truncates a message this value will exceed the buffer! Then, vprintf_buffer[len] = '\0'; will cause undefined behaviour. Add a check for truncation, which resets 'len' to the number of bytes in the buffer. Also removed the -1 from the 'size' argument to vsnprintf(), because it already accounts for the terminating '\0' byte.
1 parent 5231302 commit 07a0117

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

components/utilities/log_router/log_router.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -122,8 +122,14 @@ int esp_log_router_flash_vprintf(const char *format, va_list args)
122122

123123
// Write to all files that match the log level
124124
esp_log_router_slist_t *item;
125-
int len = vsnprintf(vprintf_buffer, sizeof(vprintf_buffer) - 1, format, args);
125+
int len = vsnprintf(vprintf_buffer, sizeof(vprintf_buffer), format, args);
126126
if (len > 0) {
127+
if (len > (sizeof(vprintf_buffer) - 1)) {
128+
int trunc_b = len - (sizeof(vprintf_buffer) - 1);
129+
log_router_debug_printf("Buffer too small, lost %d bytes\n", trunc_b);
130+
len = sizeof(vprintf_buffer) - 1;
131+
}
132+
127133
vprintf_buffer[len] = '\0';
128134
uint32_t now = (uint32_t)(esp_timer_get_time() / 1000);
129135

0 commit comments

Comments
 (0)