From b4f5c0f46c830b58e2268c10c22fc03d226f5209 Mon Sep 17 00:00:00 2001 From: Stanislav Angelovic Date: Thu, 22 Jul 2021 14:33:38 +0200 Subject: [PATCH] Catch sdbus-c++ exceptions flying from Proxy callbacks to libsystemd --- include/sdbus-c++/ConvenienceApiClasses.inl | 14 ++-------- src/Object.cpp | 6 ++--- src/Proxy.cpp | 29 ++++++++++++++++----- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/include/sdbus-c++/ConvenienceApiClasses.inl b/include/sdbus-c++/ConvenienceApiClasses.inl index 1d1f871..d144818 100644 --- a/include/sdbus-c++/ConvenienceApiClasses.inl +++ b/include/sdbus-c++/ConvenienceApiClasses.inl @@ -593,7 +593,7 @@ namespace sdbus { { reply >> args; } - catch (const sdbus::Error& e) + catch (const Error& e) { // Catch message unpack exceptions and pass them to the callback // in the expected manner to avoid propagating them up the call @@ -641,17 +641,7 @@ namespace sdbus { tuple_of_function_input_arg_types_t<_Function> signalArgs; // Deserialize input arguments from the signal message into the tuple - try - { - signal >> signalArgs; - } - catch (const sdbus::Error& e) - { - // The convenience API callback cannot handle an incoming signal with - // an unexpected payload, so catch and ignore this exception to avoid - // propagating it up the call stack to the event loop. - return; - } + signal >> signalArgs; // Invoke callback with input arguments from the tuple. sdbus::apply(callback, signalArgs); diff --git a/src/Object.cpp b/src/Object.cpp index eb79a5c..94cf8d3 100644 --- a/src/Object.cpp +++ b/src/Object.cpp @@ -337,7 +337,7 @@ int Object::sdbus_method_callback(sd_bus_message *sdbusMessage, void *userData, { callback(message); } - catch (const sdbus::Error& e) + catch (const Error& e) { sd_bus_error_set(retError, e.getName().c_str(), e.getMessage().c_str()); } @@ -371,7 +371,7 @@ int Object::sdbus_property_get_callback( sd_bus */*bus*/ { callback(reply); } - catch (const sdbus::Error& e) + catch (const Error& e) { sd_bus_error_set(retError, e.getName().c_str(), e.getMessage().c_str()); } @@ -406,7 +406,7 @@ int Object::sdbus_property_set_callback( sd_bus */*bus*/ { callback(value); } - catch (const sdbus::Error& e) + catch (const Error& e) { sd_bus_error_set(retError, e.getName().c_str(), e.getMessage().c_str()); } diff --git a/src/Proxy.cpp b/src/Proxy.cpp index 7796992..c6d0618 100644 --- a/src/Proxy.cpp +++ b/src/Proxy.cpp @@ -240,15 +240,22 @@ int Proxy::sdbus_async_reply_handler(sd_bus_message *sdbusMessage, void *userDat proxy.m_CurrentlyProcessedMessage.store(nullptr, std::memory_order_relaxed); }; - const auto* error = sd_bus_message_get_error(sdbusMessage); - if (error == nullptr) + try { - asyncCallData->callback(message, nullptr); + const auto* error = sd_bus_message_get_error(sdbusMessage); + if (error == nullptr) + { + asyncCallData->callback(message, nullptr); + } + else + { + Error exception(error->name, error->message); + asyncCallData->callback(message, &exception); + } } - else + catch (const Error&) { - sdbus::Error exception(error->name, error->message); - asyncCallData->callback(message, &exception); + // Intentionally left blank -- sdbus-c++ exceptions shall not bubble up to the underlying C sd-bus library } return 1; @@ -268,7 +275,15 @@ int Proxy::sdbus_signal_handler(sd_bus_message *sdbusMessage, void *userData, sd signalData->proxy.m_CurrentlyProcessedMessage.store(nullptr, std::memory_order_relaxed); }; - signalData->callback(message); + try + { + signalData->callback(message); + } + catch (const Error&) + { + // Intentionally left blank -- sdbus-c++ exceptions shall not bubble up to the underlying C sd-bus library + } + return 0; }