From c8609af6cea71ee381588cc35efab1d681684535 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Mar 2026 15:47:56 -1000 Subject: [PATCH 1/3] [sensor] Replace truncation guard with ESPHOME_DEBUG_ASSERT --- esphome/components/sensor/filter.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h index 05ffa4cb6e..516b5e7a89 100644 --- a/esphome/components/sensor/filter.h +++ b/esphome/components/sensor/filter.h @@ -348,10 +348,9 @@ template class ValueListFilter : public Filter { explicit ValueListFilter(std::initializer_list> values) { size_t i = 0; for (const auto &v : values) { - if (i >= N) - break; this->values_[i++] = v; } + ESPHOME_DEBUG_ASSERT(i == N); } /// Check if sensor value matches any configured value (with accuracy rounding) From 70a9c2f8b6d4408af1b91592af5e77350f3c7335 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Mar 2026 15:49:57 -1000 Subject: [PATCH 2/3] [sensor] Assert size before writing, not after --- esphome/components/sensor/filter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h index 516b5e7a89..df2b8c0c42 100644 --- a/esphome/components/sensor/filter.h +++ b/esphome/components/sensor/filter.h @@ -346,11 +346,11 @@ uint32_t get_loop_component_start_time(); template class ValueListFilter : public Filter { protected: explicit ValueListFilter(std::initializer_list> values) { + ESPHOME_DEBUG_ASSERT(values.size() == N); size_t i = 0; for (const auto &v : values) { this->values_[i++] = v; } - ESPHOME_DEBUG_ASSERT(i == N); } /// Check if sensor value matches any configured value (with accuracy rounding) From 494d431bf8cc7c34a9ee0ddc627fd41a0b3ccae2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Mar 2026 16:14:45 -1000 Subject: [PATCH 3/3] Use init_array_from helper for optimal codegen Uses memcpy for trivially copyable types, element-wise copy otherwise. ESPHOME_DEBUG_ASSERT catches size mismatches in integration tests. --- esphome/components/sensor/filter.h | 6 +----- esphome/core/helpers.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/esphome/components/sensor/filter.h b/esphome/components/sensor/filter.h index df2b8c0c42..87a309ffb3 100644 --- a/esphome/components/sensor/filter.h +++ b/esphome/components/sensor/filter.h @@ -346,11 +346,7 @@ uint32_t get_loop_component_start_time(); template class ValueListFilter : public Filter { protected: explicit ValueListFilter(std::initializer_list> values) { - ESPHOME_DEBUG_ASSERT(values.size() == N); - size_t i = 0; - for (const auto &v : values) { - this->values_[i++] = v; - } + init_array_from(this->values_, values); } /// Check if sensor value matches any configured value (with accuracy rounding) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 82c6b3833c..51feaa57c5 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -497,6 +497,21 @@ template::max()> index_type capacity_{0}; }; +/// Initialize a std::array from an initializer_list. Uses memcpy for trivially copyable types (optimal codegen), +/// falls back to element-wise copy for non-trivially copyable types (e.g. TemplatableValue). +/// N is set by code generation; ESPHOME_DEBUG_ASSERT catches mismatches in debug/integration tests. +template inline void init_array_from(std::array &dest, std::initializer_list src) { + ESPHOME_DEBUG_ASSERT(src.size() == N); + if constexpr (std::is_trivially_copyable_v) { + __builtin_memcpy(dest.data(), src.begin(), N * sizeof(T)); + } else { + size_t i = 0; + for (const auto &v : src) { + dest[i++] = v; + } + } +} + /// Fixed-capacity vector - allocates once at runtime, never reallocates /// This avoids std::vector template overhead (_M_realloc_insert, _M_default_append) /// when size is known at initialization but not at compile time