From 66a2a0d62ea8585656c420e11b49ec1ad9893bb0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Feb 2026 19:20:24 -0600 Subject: [PATCH] only during data --- esphome/components/api/api_frame_helper_noise.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/esphome/components/api/api_frame_helper_noise.cpp b/esphome/components/api/api_frame_helper_noise.cpp index 34b5ba3c3f..882a9e86f4 100644 --- a/esphome/components/api/api_frame_helper_noise.cpp +++ b/esphome/components/api/api_frame_helper_noise.cpp @@ -201,10 +201,13 @@ APIError APINoiseFrameHelper::try_read_frame_() { return (state_ == State::DATA) ? APIError::BAD_DATA_PACKET : APIError::BAD_HANDSHAKE_PACKET_LEN; } - // Reserve space for body (+1 for null terminator so protobuf StringRef fields - // can be safely null-terminated in-place after decode) - if (this->rx_buf_.size() != msg_size + 1) { - this->rx_buf_.resize(msg_size + 1); + // Reserve space for body (+1 for null terminator in DATA state so protobuf + // StringRef fields can be safely null-terminated in-place after decode. + // During handshake, rx_buf_.size() is used in prologue construction, so + // the buffer must be exactly msg_size to avoid prologue mismatch.) + uint16_t alloc_size = msg_size + (state_ == State::DATA ? 1 : 0); + if (this->rx_buf_.size() != alloc_size) { + this->rx_buf_.resize(alloc_size); } if (rx_buf_len_ < msg_size) {