From 9bdefc98b185b73aad571b029b4b746fb8248b79 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 15 Jan 2026 18:17:42 -1000 Subject: [PATCH 1/2] bot concerns --- esphome/core/helpers.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 355947fe5ec..e33027f39fe 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -143,19 +143,29 @@ template class SmallInlineBuffer { delete[] this->heap_; } - // Move constructor - memcpy is safe because union is zero-initialized + // Move constructor SmallInlineBuffer(SmallInlineBuffer &&other) noexcept : len_(other.len_) { - memcpy(this->inline_, other.inline_, InlineSize); - other.len_ = 0; // Mark as empty so other's destructor is no-op + if (other.is_inline_()) { + memcpy(this->inline_, other.inline_, this->len_); + } else { + this->heap_ = other.heap_; + other.heap_ = nullptr; + } + other.len_ = 0; } - // Move assignment - memcpy is safe because union is zero-initialized + // Move assignment SmallInlineBuffer &operator=(SmallInlineBuffer &&other) noexcept { if (this != &other) { if (!this->is_inline_()) delete[] this->heap_; this->len_ = other.len_; - memcpy(this->inline_, other.inline_, InlineSize); + if (other.is_inline_()) { + memcpy(this->inline_, other.inline_, this->len_); + } else { + this->heap_ = other.heap_; + other.heap_ = nullptr; + } other.len_ = 0; } return *this; @@ -188,7 +198,7 @@ template class SmallInlineBuffer { size_t len_{0}; union { - uint8_t inline_[InlineSize]{}; // Zero-init for safe memcpy in move ops + uint8_t inline_[InlineSize]{}; // Zero-init ensures clean initial state uint8_t *heap_; }; }; From 42b9863cd3429b25d431d3d095dff8297b1872d3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 15 Jan 2026 18:19:15 -1000 Subject: [PATCH 2/2] bot concerns --- esphome/core/helpers.h | 1 + 1 file changed, 1 insertion(+) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index e33027f39fe..39a4c5cd785 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -180,6 +180,7 @@ template class SmallInlineBuffer { // Free existing heap allocation if switching from heap to inline or different heap size if (!this->is_inline_() && (size <= InlineSize || size != this->len_)) { delete[] this->heap_; + this->heap_ = nullptr; // Defensive: prevent use-after-free if logic changes } // Allocate new heap buffer if needed if (size > InlineSize && (this->is_inline_() || size != this->len_)) {