From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Kent Gibson <warthog618@gmail.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Geert Uytterhoeven <geert@linux-m68k.org>,
Jack Winch <sunt.un.morcov@gmail.com>,
Helmut Grohne <helmut.grohne@intenta.de>,
Linus Walleij <linus.walleij@linaro.org>
Cc: linux-gpio@vger.kernel.org,
Bartosz Golaszewski <bgolaszewski@baylibre.com>
Subject: [libgpiod][PATCH 01/14] bindings: cxx: check for error from gpiod_line_bulk_new()
Date: Thu, 10 Dec 2020 14:23:02 +0100 [thread overview]
Message-ID: <20201210132315.5785-2-brgl@bgdev.pl> (raw)
In-Reply-To: <20201210132315.5785-1-brgl@bgdev.pl>
From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
We call gpiod_line_bulk_new() in C++ bindings but never check its return
value. This function can fail so add a private method to line_bulk that
calls it and throws an exception if it returns NULL.
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
bindings/cxx/gpiod.hpp | 1 +
bindings/cxx/line_bulk.cpp | 22 ++++++++++++++++------
2 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/bindings/cxx/gpiod.hpp b/bindings/cxx/gpiod.hpp
index a4ef7c9..8c8e6c9 100644
--- a/bindings/cxx/gpiod.hpp
+++ b/bindings/cxx/gpiod.hpp
@@ -869,6 +869,7 @@ private:
using line_bulk_ptr = ::std::unique_ptr<::gpiod_line_bulk, line_bulk_deleter>;
+ line_bulk_ptr make_line_bulk_ptr(void) const;
line_bulk_ptr to_line_bulk(void) const;
::std::vector<line> _m_bulk;
diff --git a/bindings/cxx/line_bulk.cpp b/bindings/cxx/line_bulk.cpp
index c24e91d..1de90eb 100644
--- a/bindings/cxx/line_bulk.cpp
+++ b/bindings/cxx/line_bulk.cpp
@@ -256,8 +256,7 @@ line_bulk line_bulk::event_wait(const ::std::chrono::nanoseconds& timeout) const
this->throw_if_empty();
line::chip_guard lock_chip(this->_m_bulk.front());
- auto ev_bulk = ::gpiod_line_bulk_new(this->size());
- line_bulk_ptr ev_bulk_deleter(ev_bulk);
+ auto ev_bulk = this->make_line_bulk_ptr();
auto bulk = this->to_line_bulk();
::timespec ts;
line_bulk ret;
@@ -266,16 +265,16 @@ line_bulk line_bulk::event_wait(const ::std::chrono::nanoseconds& timeout) const
ts.tv_sec = timeout.count() / 1000000000ULL;
ts.tv_nsec = timeout.count() % 1000000000ULL;
- rv = ::gpiod_line_event_wait_bulk(bulk.get(), ::std::addressof(ts), ev_bulk);
+ rv = ::gpiod_line_event_wait_bulk(bulk.get(), ::std::addressof(ts), ev_bulk.get());
if (rv < 0) {
throw ::std::system_error(errno, ::std::system_category(),
"error polling for events");
} else if (rv > 0) {
auto chip = this->_m_bulk[0].get_chip();
- auto num_lines = ::gpiod_line_bulk_num_lines(ev_bulk);
+ auto num_lines = ::gpiod_line_bulk_num_lines(ev_bulk.get());
for (unsigned int i = 0; i < num_lines; i++)
- ret.append(line(::gpiod_line_bulk_get_line(ev_bulk, i), chip));
+ ret.append(line(::gpiod_line_bulk_get_line(ev_bulk.get(), i), chip));
}
return ret;
@@ -340,10 +339,21 @@ void line_bulk::throw_if_empty(void) const
throw ::std::logic_error("line_bulk not holding any GPIO lines");
}
-line_bulk::line_bulk_ptr line_bulk::to_line_bulk(void) const
+line_bulk::line_bulk_ptr line_bulk::make_line_bulk_ptr(void) const
{
line_bulk_ptr bulk(::gpiod_line_bulk_new(this->size()));
+ if (!bulk)
+ throw ::std::system_error(errno, ::std::system_category(),
+ "unable to allocate new bulk object");
+
+ return bulk;
+}
+
+line_bulk::line_bulk_ptr line_bulk::to_line_bulk(void) const
+{
+ line_bulk_ptr bulk = this->make_line_bulk_ptr();
+
for (auto& it: this->_m_bulk)
::gpiod_line_bulk_add_line(bulk.get(), it._m_line);
--
2.29.1
next prev parent reply other threads:[~2020-12-10 13:24 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-10 13:23 [libgpiod][PATCH 00/14] treewide: start shaving off cruft for v2.0 Bartosz Golaszewski
2020-12-10 13:23 ` Bartosz Golaszewski [this message]
2020-12-10 13:23 ` [libgpiod][PATCH 02/14] build: drop the message about tests having been built successfully Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 03/14] core: export gpiod_is_gpiochip_device() Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 04/14] bulk: drop the limit on the max number of lines Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 05/14] core: drop line iterators Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 06/14] treewide: kill opening chips by label Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 07/14] API: move gpiod_line_get_chip() to line attributes section Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 08/14] core: kill gpiod_line_close_chip() Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 09/14] core: kill gpiod_line_get() Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 10/14] treewide: kill global line lookup Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 11/14] treewide: kill find_lines() Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 12/14] core: rework gpiod_chip_find_line() Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 13/14] build: add a configure switch for building examples Bartosz Golaszewski
2020-12-10 13:23 ` [libgpiod][PATCH 14/14] core: kill chip iterators Bartosz Golaszewski
2020-12-10 13:56 ` [libgpiod][PATCH 00/14] treewide: start shaving off cruft for v2.0 Andy Shevchenko
2020-12-11 8:38 ` Bartosz Golaszewski
2020-12-11 14:31 ` Andy Shevchenko
2020-12-11 14:33 ` Bartosz Golaszewski
2020-12-11 14:58 ` Andy Shevchenko
2020-12-11 15:06 ` Bartosz Golaszewski
2020-12-14 15:02 ` Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201210132315.5785-2-brgl@bgdev.pl \
--to=brgl@bgdev.pl \
--cc=andriy.shevchenko@linux.intel.com \
--cc=bgolaszewski@baylibre.com \
--cc=geert@linux-m68k.org \
--cc=helmut.grohne@intenta.de \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=sunt.un.morcov@gmail.com \
--cc=warthog618@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.