aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick Boettcher <p@yai.se>2019-02-03 13:29:14 +0100
committerBartosz Golaszewski <bgolaszewski@baylibre.com>2019-02-04 10:01:44 +0100
commite474b7792adce5ffa81e964ffd5a6302388c8b3a (patch)
treeee42391e46071aee5da517259e8d73b1e204c9ea
parentf3637ade7908be8a7e44b029d2a4d54e830a809a (diff)
downloadlibgpiod-e474b7792adce5ffa81e964ffd5a6302388c8b3a.tar.gz
bindings: cxx: do not initialize a chip's shared_ptr with nullptr
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>
-rw-r--r--bindings/cxx/iter.cpp6
1 files changed, 4 insertions, 2 deletions
diff --git a/bindings/cxx/iter.cpp b/bindings/cxx/iter.cpp
index 0f110576..39e738d7 100644
--- a/bindings/cxx/iter.cpp
+++ b/bindings/cxx/iter.cpp
@@ -58,10 +58,12 @@ bool chip_iter::operator!=(const chip_iter& rhs) const noexcept
}
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)