mirror of
https://github.com/esphome/esphome.git
synced 2026-07-10 17:05:36 +00:00
[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.
This commit is contained in:
@@ -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<uint8_t>(std::min(res, uint32_t(255)));
|
||||
}
|
||||
|
||||
} // namespace esphome::light
|
||||
|
||||
Reference in New Issue
Block a user