From 2626a851fbf1e7feb85c1661373941359bd72f08 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 13 Oct 2025 15:30:18 -1000 Subject: [PATCH] cleanup --- esphome/core/helpers.h | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 349ed663ad..3ca62a68cb 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -170,8 +170,11 @@ template class FixedVector { // Helper to destroy all elements without freeing memory void destroy_elements_() { - for (size_t i = 0; i < size_; i++) { - data_[i].~T(); + // Only call destructors for non-trivially destructible types + if constexpr (!std::is_trivially_destructible::value) { + for (size_t i = 0; i < size_; i++) { + data_[i].~T(); + } } } @@ -221,7 +224,7 @@ template class FixedVector { reset_(); if (n > 0) { // Allocate raw memory without calling constructors - // sizeof(T) is correct here - when T is a pointer type, we want the pointer size + // sizeof(T) is correct here for any type T (value types, pointers, etc.) // NOLINTNEXTLINE(bugprone-sizeof-expression) data_ = static_cast(::operator new(n * sizeof(T))); capacity_ = n; @@ -265,15 +268,11 @@ template class FixedVector { /// Emplace element without bounds checking - constructs in-place /// Caller must ensure sufficient capacity was allocated via init() /// Returns reference to the newly constructed element - /// Silently ignores emplaces beyond capacity (returns reference to last element) + /// NOTE: Caller MUST ensure size_ < capacity_ before calling T &emplace_back() { - if (size_ < capacity_) { - // Use placement new to default-construct the object in pre-allocated memory - new (&data_[size_]) T(); - size_++; - return data_[size_ - 1]; - } - // Beyond capacity - return reference to last element to avoid crash + // Use placement new to default-construct the object in pre-allocated memory + new (&data_[size_]) T(); + size_++; return data_[size_ - 1]; }