diff --git a/esphome/components/api/api_buffer.h b/esphome/components/api/api_buffer.h index 13dd4e5610..8feaad5183 100644 --- a/esphome/components/api/api_buffer.h +++ b/esphome/components/api/api_buffer.h @@ -26,8 +26,13 @@ inline std::unique_ptr make_buffer(size_t n) { /// making the zero-fill pure waste. For the receive buffer, bytes are /// overwritten by socket reads. /// +/// Designed for bulk clear/resize/overwrite patterns. grow_() allocates +/// exactly the requested size (no growth factor) since callers resize to +/// known sizes rather than appending incrementally. +/// /// Safe because: callers always write exactly the number of bytes they -/// resize for, and debug_check_bounds_ validates writes in debug builds. +/// resize for. In the protobuf write path, debug_check_bounds_ validates +/// writes in debug builds. class APIBuffer { public: void clear() { this->size_ = 0; } diff --git a/esphome/components/api/api_frame_helper.h b/esphome/components/api/api_frame_helper.h index 2ec2b72c93..f0666debee 100644 --- a/esphome/components/api/api_frame_helper.h +++ b/esphome/components/api/api_frame_helper.h @@ -178,7 +178,6 @@ class APIFrameHelper { // rx_buf_len_ tracks bytes read so far; if non-zero, we're mid-frame // and clearing would lose partially received data. if (this->rx_buf_len_ == 0) { - // Use swap trick since shrink_to_fit() is non-binding and may be ignored this->rx_buf_.release(); } } diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index aabea3485f..69fc26cc00 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -2,6 +2,7 @@ #include "esphome/core/defines.h" #ifdef USE_API +#include "api_buffer.h" #include "api_noise_context.h" #include "api_pb2.h" #include "api_pb2_service.h"