Fix crash when using addressable_set with out-of-range indices (#2120)

This commit is contained in:
Oxan van Leeuwen 2021-08-05 01:30:32 +02:00 committed by GitHub
parent 0d104776bc
commit cb21c7c18d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View file

@ -159,8 +159,14 @@ template<typename... Ts> class AddressableSet : public Action<Ts...> {
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);

View file

@ -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)