Fix exception handling of OnReceivedResult() 38/251338/3
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 13 Jan 2021 00:36:46 +0000 (09:36 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 13 Jan 2021 04:30:19 +0000 (13:30 +0900)
To get an error of gdbus properly, we have to use g_dbus_message_to_gerror().

Change-Id: Ic733e2c9d808668c456758f5ae2876c8543e6358
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/fdbroker-internal.cc

index 285b335..033157f 100644 (file)
@@ -839,13 +839,19 @@ void FdBroker::OnResultReceived(GObject* source_object,
   GError* err = nullptr;
   GDBusMessage* reply = g_dbus_connection_send_message_with_reply_finish(conn,
       res, &err);
-
-  if (err && err->code == G_IO_ERROR_CANCELLED) {
-    g_error_free(err);
-    _E("IO error cancelled");
+  if (reply == nullptr) {
+    if (err) {
+      if (err->code == G_IO_ERROR_CANCELLED)
+        _E("IO error cancelled");
+      g_error_free(err);
+    } else {
+      _E("g_dbus_connection_send_message_with_reply_finish() is failed");
+    }
     return;
   }
 
+  auto reply_ptr = std::unique_ptr<GDBusMessage, decltype(g_object_unref)*>(
+      reply, g_object_unref);
   auto* ptr = static_cast<std::weak_ptr<FdBroker>*>(user_data);
   auto broker = ptr->lock();
   if (broker == nullptr) {
@@ -860,11 +866,10 @@ void FdBroker::OnResultReceived(GObject* source_object,
   }
 
   IEventWatcher* watcher = broker->watcher_;
-
-  if (err) {
-    // LCOV_EXCL_START
-    watcher->OnPortDisconnected(true);
+  if (g_dbus_message_to_gerror(reply, &err)) {
+    _E("Failed to send message. error(%s)", err->message);
     g_error_free(err);
+    watcher->OnPortDisconnected(true);
     if (broker->cancellable_) {
       _W("Cancel the send request");
       g_cancellable_cancel(broker->cancellable_);
@@ -872,27 +877,17 @@ void FdBroker::OnResultReceived(GObject* source_object,
       broker->cancellable_ = nullptr;
     }
     return;
-    // LCOV_EXCL_STOP
-  }
-
-  if (reply == nullptr) {
-    _W("Null reply");  // LCOV_EXCL_LINE
-    return;  // LCOV_EXCL_LINE
   }
 
   GVariant* reply_body = g_dbus_message_get_body(reply);
   if (reply_body == nullptr) {
-    // LCOV_EXCL_START
     _E("g_dbus_message_get_body() is failed");
     watcher->OnPortDisconnected();
-    g_object_unref(reply);
     return;
-    // LCOV_EXCL_STOP
   }
 
   int ret;
   g_variant_get(reply_body, "(i)", &ret);
-  g_object_unref(reply);
   if (ret != 0) {
     _E("Access Denied[sender_appid : %s]", broker->watch_appid_.c_str());
     watcher->OnPortRejected(RPC_PORT_ERROR_PERMISSION_DENIED);