Dbus: added exception handlers in destructors. 35/320635/1 accepted/tizen/unified/20250312.145441 accepted/tizen/unified/x/20250312.134154
authorMichal Szczecinski <m.szczecinsk@partner.samsung.com>
Wed, 5 Mar 2025 07:04:12 +0000 (08:04 +0100)
committerMichal Szczecinski <m.szczecinsk@partner.samsung.com>
Wed, 5 Mar 2025 07:04:12 +0000 (08:04 +0100)
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
src/service/DBus.hpp

index 2a8e3433f387bdbf1e45addde4df7f84076f615e..6aacf2f7732e4f8ecace4b578c5a1dca7f0d45ae 100644 (file)
@@ -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<void(std::string, std::string)> fnc_)
index 70d6d87d729ba5ef2ad6ac5cf45532d767e34237..cb9b07684ea18eb246a7d5ef188c12feefd6676a 100644 (file)
@@ -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<std::function<void()>> 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> destructorObject{new DestructorObject()};