From 701dd98e75ffa2dc96cf2a9ceb6631ac8c8850dc Mon Sep 17 00:00:00 2001 From: Michal Szczecinski Date: Wed, 5 Mar 2025 08:04:12 +0100 Subject: [PATCH] Dbus: added exception handlers in destructors. To avoid std::terminate() in case of exception in destructor, this patch adds exception handlers. Now instead of terminating application, an error will be printed. Change-Id: Ia5db61559b649df8eb1803ef730c518e96e9f283 --- src/service/DBus.cpp | 19 ++++++++++++++----- src/service/DBus.hpp | 14 +++++++++++--- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/service/DBus.cpp b/src/service/DBus.cpp index 2a8e343..6aacf2f 100644 --- a/src/service/DBus.cpp +++ b/src/service/DBus.cpp @@ -242,9 +242,13 @@ struct DefaultDBusWrapper : public DBusWrapper ProxyImpl(Eldbus_Proxy *Value, bool EraseOnExit = false) : Value(Value), EraseOnExit(EraseOnExit) {} ~ProxyImpl() { - for (auto &it : destructors) - if (it) - it(); + try { + for (auto &it : destructors) + if (it) + it(); + } catch (...) { + DBUS_DEBUG("ProxyImpl: Exception in destructor"); + } } }; DEFINE_GS(Proxy, Eldbus_Proxy, ) @@ -756,8 +760,13 @@ OwnerNameCallbackHandle &OwnerNameCallbackHandle::operator=(OwnerNameCallbackHan OwnerNameCallbackHandle::~OwnerNameCallbackHandle() { - if (callback_) - callback_(); + try { + if (callback_) { + callback_(); + } + } catch (...) { + DBUS_DEBUG("Exception in OwnerNameCallbackHandle destructor"); + } } OwnerNameCallbackHandle DBus::registerNameOwnerChangedCallback(DBusWrapper::ConnectionPtr connection, const std::string &bus, std::function fnc_) diff --git a/src/service/DBus.hpp b/src/service/DBus.hpp index 70d6d87..cb9b076 100644 --- a/src/service/DBus.hpp +++ b/src/service/DBus.hpp @@ -498,7 +498,11 @@ namespace DBus } const char *what() const noexcept override { - text = temp->str(); + try { + text = temp->str(); + } catch (...) { + return "Exception information is not available due to memory allocation issue"; + } return text.c_str(); } }; @@ -3408,8 +3412,12 @@ namespace DBus std::vector> destructors; ~DestructorObject() { - for (auto &a : destructors) - a(); + try { + for (auto &a : destructors) + a(); + } catch (...) { + DBUS_DEBUG("Fatal issue: unhandled exception in destructor"); + } } }; std::unique_ptr destructorObject{new DestructorObject()}; -- 2.34.1