From 2f7270cf8f6f069191920f81503466361e42515b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 18 Jan 2026 18:36:32 -1000 Subject: [PATCH] [uart] Replace unsafe sprintf with buf_append_printf in debugger (#13288) --- esphome/components/uart/uart_debugger.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/esphome/components/uart/uart_debugger.cpp b/esphome/components/uart/uart_debugger.cpp index b51a57d68e..5490154d01 100644 --- a/esphome/components/uart/uart_debugger.cpp +++ b/esphome/components/uart/uart_debugger.cpp @@ -107,7 +107,7 @@ void UARTDebug::log_hex(UARTDirection direction, std::vector bytes, uin if (i > 0) { res += separator; } - sprintf(buf, "%02X", bytes[i]); + buf_append_printf(buf, sizeof(buf), 0, "%02X", bytes[i]); res += buf; } ESP_LOGD(TAG, "%s", res.c_str()); @@ -147,7 +147,7 @@ void UARTDebug::log_string(UARTDirection direction, std::vector bytes) } else if (bytes[i] == 92) { res += "\\\\"; } else if (bytes[i] < 32 || bytes[i] > 127) { - sprintf(buf, "\\x%02X", bytes[i]); + buf_append_printf(buf, sizeof(buf), 0, "\\x%02X", bytes[i]); res += buf; } else { res += bytes[i]; @@ -166,11 +166,13 @@ void UARTDebug::log_int(UARTDirection direction, std::vector bytes, uin } else { res += ">>> "; } + char buf[4]; // max 3 digits for uint8_t (255) + null for (size_t i = 0; i < len; i++) { if (i > 0) { res += separator; } - res += to_string(bytes[i]); + buf_append_printf(buf, sizeof(buf), 0, "%u", bytes[i]); + res += buf; } ESP_LOGD(TAG, "%s", res.c_str()); delay(10); @@ -189,7 +191,7 @@ void UARTDebug::log_binary(UARTDirection direction, std::vector bytes, if (i > 0) { res += separator; } - sprintf(buf, "0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", BYTE_TO_BINARY(bytes[i]), bytes[i]); + buf_append_printf(buf, sizeof(buf), 0, "0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", BYTE_TO_BINARY(bytes[i]), bytes[i]); res += buf; } ESP_LOGD(TAG, "%s", res.c_str());