From 76c8eeede987807ca31ea0a24182fface63ba2f8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 13 Apr 2026 15:08:36 -1000 Subject: [PATCH] [light] Fix uint16_t overflow in color_uncorrect_channel_ When max_brightness and local_brightness_ are small but non-zero, the intermediate (uncorrected / max_brightness) * 255 can exceed 65535 before the std::min(255) clamp runs, producing an incorrect low result. Widen intermediates to uint32_t. Copilot review catch on #15727. --- esphome/components/light/esp_color_correction.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/esphome/components/light/esp_color_correction.cpp b/esphome/components/light/esp_color_correction.cpp index 03ac96567c..e793226bb1 100644 --- a/esphome/components/light/esp_color_correction.cpp +++ b/esphome/components/light/esp_color_correction.cpp @@ -31,9 +31,11 @@ Color ESPColorCorrection::color_uncorrect(Color color) const { uint8_t ESPColorCorrection::color_uncorrect_channel_(uint8_t value, uint8_t max_brightness) const { if (max_brightness == 0 || this->local_brightness_ == 0) return 0; - uint16_t uncorrected = this->gamma_uncorrect_(value) * 255UL; - uint16_t res = ((uncorrected / max_brightness) * 255UL) / this->local_brightness_; - return (uint8_t) std::min(res, uint16_t(255)); + // Use 32-bit intermediates: when max_brightness and local_brightness_ are small but non-zero, + // (uncorrected / max_brightness) * 255 can exceed 65535 before the std::min(255) clamp runs. + uint32_t uncorrected = this->gamma_uncorrect_(value) * 255UL; + uint32_t res = ((uncorrected / max_brightness) * 255UL) / this->local_brightness_; + return static_cast(std::min(res, uint32_t(255))); } } // namespace esphome::light