A shared_ptr initialized with nullptr will still call the deleter which
causes a NULL-pointer dereference if we create a chip_iter when there
are no GPIO chips in the system.
Only initialize the current chip object in chip_iter if
gpiod_chip_iter_next_noclose() returned a valid pointer.
Signed-off-by: Patrick Boettcher <p@yai.se>
[Bartosz:
- tweaked the commit message
- use std::move when assigning the chip object]
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
}
chip_iter::chip_iter(::gpiod_chip_iter *iter)
- : _m_iter(iter, chip_iter_deleter),
- _m_current(chip(::gpiod_chip_iter_next_noclose(this->_m_iter.get())))
+ : _m_iter(iter, chip_iter_deleter)
{
+ ::gpiod_chip* first = ::gpiod_chip_iter_next_noclose(this->_m_iter.get());
+ if (first != nullptr)
+ this->_m_current = ::std::move(chip(first));
}
chip_iter& chip_iter::operator++(void)