From a42820dc26f43efb5d23dc6d24836a6cdb120797 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 30 Dec 2025 10:49:02 -1000 Subject: [PATCH] should never happen but ok --- esphome/components/api/api_connection.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 3bc0e73211..62072969fa 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1714,11 +1714,15 @@ void APIConnection::on_home_assistant_state_response(const HomeAssistantStateRes // Create null-terminated state for callback (parse_number needs null-termination) // HA state max length is 255, so 256 byte buffer covers all cases char state_buf[256]; - if (msg.state_len > 0) { - memcpy(state_buf, msg.state, msg.state_len); + size_t copy_len = msg.state_len; + if (copy_len >= sizeof(state_buf)) { + copy_len = sizeof(state_buf) - 1; // Truncate to leave space for null terminator } - state_buf[msg.state_len] = '\0'; - it.callback(StringRef(state_buf, msg.state_len)); + if (copy_len > 0) { + memcpy(state_buf, msg.state, copy_len); + } + state_buf[copy_len] = '\0'; + it.callback(StringRef(state_buf, copy_len)); } } #endif