From 6610e5304844fa20ff9a6246f4aa9cb61a10b6a1 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Sun, 13 May 2018 23:34:38 +0200 Subject: [PATCH] bindings: cxx: fix the return value of line_bulk::find_lines() We should return an invalid line_bulk object if we failed to look up at least one line from the provided list of names. Signed-off-by: Bartosz Golaszewski --- bindings/cxx/chip.cpp | 12 ++++++++++-- bindings/cxx/examples/gpiod_cxx_tests.cpp | 12 ++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/bindings/cxx/chip.cpp b/bindings/cxx/chip.cpp index 15136ca..f4c92ef 100644 --- a/bindings/cxx/chip.cpp +++ b/bindings/cxx/chip.cpp @@ -158,9 +158,17 @@ line_bulk chip::get_all_lines(void) const line_bulk chip::find_lines(const ::std::vector<::std::string>& names) const { line_bulk lines; + line line; - for (auto& it: names) - lines.append(this->find_line(it)); + for (auto& it: names) { + line = this->find_line(it); + if (!line) { + lines.clear(); + return ::std::move(lines); + } + + lines.append(line); + } return ::std::move(lines); } diff --git a/bindings/cxx/examples/gpiod_cxx_tests.cpp b/bindings/cxx/examples/gpiod_cxx_tests.cpp index 81cf62c..df4820b 100644 --- a/bindings/cxx/examples/gpiod_cxx_tests.cpp +++ b/bindings/cxx/examples/gpiod_cxx_tests.cpp @@ -185,6 +185,18 @@ void chip_line_ops(void) } TEST_CASE(chip_line_ops); +void chip_find_lines_nonexistent(void) +{ + ::gpiod::chip chip("gpiochip0"); + + auto lines = chip.find_lines({ "gpio-mockup-A-1", "nonexistent", "gpio-mockup-A-4" }); + if (lines) + throw ::std::logic_error("line_bulk object should be invalid"); + + ::std::cerr << "line_bulk invalid as expected" << ::std::endl; +} +TEST_CASE(chip_find_lines_nonexistent); + void line_info(void) { ::gpiod::chip chip("gpiochip0"); -- 2.30.2