From cb21c7c18dd6d03e488726075c06f0c1e95b1d6a Mon Sep 17 00:00:00 2001 From: Oxan van Leeuwen Date: Thu, 5 Aug 2021 01:30:32 +0200 Subject: [PATCH] Fix crash when using addressable_set with out-of-range indices (#2120) --- esphome/components/light/automation.h | 10 ++++++++-- esphome/components/light/esp_range_view.h | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/esphome/components/light/automation.h b/esphome/components/light/automation.h index a816049f08..5ec2cb626a 100644 --- a/esphome/components/light/automation.h +++ b/esphome/components/light/automation.h @@ -159,8 +159,14 @@ template class AddressableSet : public Action { void play(Ts... x) override { auto *out = (AddressableLight *) this->parent_->get_output(); - int32_t range_from = this->range_from_.value_or(x..., 0); - int32_t range_to = this->range_to_.value_or(x..., out->size() - 1) + 1; + int32_t range_from = interpret_index(this->range_from_.value_or(x..., 0), out->size()); + if (range_from < 0 || range_from >= out->size()) + range_from = 0; + + int32_t range_to = interpret_index(this->range_to_.value_or(x..., out->size() - 1) + 1, out->size()); + if (range_to < 0 || range_to >= out->size()) + range_to = out->size(); + uint8_t color_brightness = to_uint8_scale(this->color_brightness_.value_or(x..., this->parent_->remote_values.get_color_brightness())); auto range = out->range(range_from, range_to); diff --git a/esphome/components/light/esp_range_view.h b/esphome/components/light/esp_range_view.h index f2cc347176..f4a7980543 100644 --- a/esphome/components/light/esp_range_view.h +++ b/esphome/components/light/esp_range_view.h @@ -11,6 +11,9 @@ int32_t interpret_index(int32_t index, int32_t size); class AddressableLight; class ESPRangeIterator; +/** + * A half-open range of LEDs, inclusive of the begin index and exclusive of the end index, using zero-based numbering. + */ class ESPRangeView : public ESPColorSettable { public: ESPRangeView(AddressableLight *parent, int32_t begin, int32_t end)