From f47799a645e6f5dddac82ac516362956ba01555a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Mar 2026 15:48:09 -1000 Subject: [PATCH 1/3] [text_sensor] Replace truncation guard with ESPHOME_DEBUG_ASSERT --- esphome/components/text_sensor/filter.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/esphome/components/text_sensor/filter.h b/esphome/components/text_sensor/filter.h index 2a90a6ad50..f4bb0228c5 100644 --- a/esphome/components/text_sensor/filter.h +++ b/esphome/components/text_sensor/filter.h @@ -133,10 +133,9 @@ template class SubstituteFilter : public Filter { explicit SubstituteFilter(const std::initializer_list &substitutions) { size_t i = 0; for (const auto &s : substitutions) { - if (i >= N) - break; this->substitutions_[i++] = s; } + ESPHOME_DEBUG_ASSERT(i == N); } bool new_value(std::string &value) override { return substitute_filter_apply(this->substitutions_.data(), N, value); } From 787af59845000a62889446868c0cadd811be94a5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Mar 2026 15:55:29 -1000 Subject: [PATCH 2/3] [text_sensor] Assert size before writing, not after --- esphome/components/text_sensor/filter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/text_sensor/filter.h b/esphome/components/text_sensor/filter.h index f4bb0228c5..ae3307f5ed 100644 --- a/esphome/components/text_sensor/filter.h +++ b/esphome/components/text_sensor/filter.h @@ -131,11 +131,11 @@ bool substitute_filter_apply(const Substitution *substitutions, size_t count, st template class SubstituteFilter : public Filter { public: explicit SubstituteFilter(const std::initializer_list &substitutions) { + ESPHOME_DEBUG_ASSERT(substitutions.size() == N); size_t i = 0; for (const auto &s : substitutions) { this->substitutions_[i++] = s; } - ESPHOME_DEBUG_ASSERT(i == N); } bool new_value(std::string &value) override { return substitute_filter_apply(this->substitutions_.data(), N, value); } From c4aed6f7e54b008ac629fc2d900efa8c071347aa Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Mar 2026 16:14:57 -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/text_sensor/filter.h | 6 +----- esphome/core/helpers.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/esphome/components/text_sensor/filter.h b/esphome/components/text_sensor/filter.h index ae3307f5ed..449de7c28f 100644 --- a/esphome/components/text_sensor/filter.h +++ b/esphome/components/text_sensor/filter.h @@ -131,11 +131,7 @@ bool substitute_filter_apply(const Substitution *substitutions, size_t count, st template class SubstituteFilter : public Filter { public: explicit SubstituteFilter(const std::initializer_list &substitutions) { - ESPHOME_DEBUG_ASSERT(substitutions.size() == N); - size_t i = 0; - for (const auto &s : substitutions) { - this->substitutions_[i++] = s; - } + init_array_from(this->substitutions_, substitutions); } bool new_value(std::string &value) override { return substitute_filter_apply(this->substitutions_.data(), N, value); } 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