From: Daniel Elstner Date: Wed, 14 Oct 2015 18:56:51 +0000 (+0200) Subject: C++: Declare all callbacks invoked from C noexcept X-Git-Tag: libsigrok-0.4.0~176 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=0ab8e5d22bbb82674a825125c4cb23b8f3b78858;p=libsigrok.git C++: Declare all callbacks invoked from C noexcept If one of these functions does throw an exception, std::terminate() will be called. Without this, the behavior is undefined since the C stack is not prepared to deal with exceptions. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 1b53d0a7..f1651287 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -74,7 +74,7 @@ ResourceReader::~ResourceReader() } SR_PRIV int ResourceReader::open_callback(struct sr_resource *res, - const char *name, void *cb_data) + const char *name, void *cb_data) noexcept { try { auto *const reader = static_cast(cb_data); @@ -87,7 +87,8 @@ SR_PRIV int ResourceReader::open_callback(struct sr_resource *res, return SR_OK; } -SR_PRIV int ResourceReader::close_callback(struct sr_resource *res, void *cb_data) +SR_PRIV int ResourceReader::close_callback(struct sr_resource *res, + void *cb_data) noexcept { try { auto *const reader = static_cast(cb_data); @@ -101,7 +102,7 @@ SR_PRIV int ResourceReader::close_callback(struct sr_resource *res, void *cb_dat } SR_PRIV ssize_t ResourceReader::read_callback(const struct sr_resource *res, - void *buf, size_t count, void *cb_data) + void *buf, size_t count, void *cb_data) noexcept { try { auto *const reader = static_cast(cb_data); @@ -208,7 +209,8 @@ void Context::set_log_level(const LogLevel *level) check(sr_log_loglevel_set(level->id())); } -static int call_log_callback(void *cb_data, int loglevel, const char *format, va_list args) +static int call_log_callback(void *cb_data, int loglevel, + const char *format, va_list args) noexcept { const unique_ptr message {g_strdup_vprintf(format, args), &g_free}; @@ -978,7 +980,7 @@ bool Session::is_running() const return (ret != 0); } -static void session_stopped_callback(void *data) +static void session_stopped_callback(void *data) noexcept { auto *const callback = static_cast(data); (*callback)(); @@ -996,7 +998,7 @@ void Session::set_stopped_callback(SessionStoppedCallback callback) } static void datafeed_callback(const struct sr_dev_inst *sdi, - const struct sr_datafeed_packet *pkt, void *cb_data) + const struct sr_datafeed_packet *pkt, void *cb_data) noexcept { auto callback = static_cast(cb_data); callback->run(sdi, pkt); diff --git a/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp b/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp index c36ee292..59465066 100644 --- a/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp +++ b/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp @@ -246,11 +246,11 @@ private: virtual size_t read(const struct sr_resource *res, void *buf, size_t count) = 0; static SR_PRIV int open_callback(struct sr_resource *res, - const char *name, void *cb_data); + const char *name, void *cb_data) noexcept; static SR_PRIV int close_callback(struct sr_resource *res, - void *cb_data); + void *cb_data) noexcept; static SR_PRIV ssize_t read_callback(const struct sr_resource *res, - void *buf, size_t count, void *cb_data); + void *buf, size_t count, void *cb_data) noexcept; friend class Context; };