From c1fb8dae37a3ee137a442de424ea67243e32e793 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 12 Nov 2025 10:33:19 -0600 Subject: [PATCH] [light] Fix dangling reference in compute_color_mode causing memory corruption --- esphome/components/light/light_call.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/esphome/components/light/light_call.cpp b/esphome/components/light/light_call.cpp index df17f53adc..b81ecc57cb 100644 --- a/esphome/components/light/light_call.cpp +++ b/esphome/components/light/light_call.cpp @@ -406,7 +406,11 @@ void LightCall::transform_parameters_() { } } ColorMode LightCall::compute_color_mode_() { - const auto &supported_modes = this->parent_->get_traits().get_supported_color_modes(); + // Store traits locally to avoid dangling reference: get_traits() returns by value, so + // calling get_traits().get_supported_color_modes() would create a temporary LightTraits + // object, return a reference to its member, then destroy the temporary, leaving a dangling reference. + auto traits = this->parent_->get_traits(); + const auto &supported_modes = traits.get_supported_color_modes(); int supported_count = supported_modes.size(); // Some lights don't support any color modes (e.g. monochromatic light), leave it at unknown.