Add exception handling about nullptr check 54/246554/2
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 2 Nov 2020 03:03:59 +0000 (12:03 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 2 Nov 2020 03:12:41 +0000 (12:12 +0900)
Change-Id: Iad6efe39d06c4b00f4d62e367167e05dde93cd47
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
notification-ex/dbus_sender.cc

index 9d6bcdb..2c88ec6 100644 (file)
@@ -143,21 +143,47 @@ std::list<Bundle> DBusSender::Request(const IEventInfo& info) {
   Bundle serialized = info.Serialize();
 
   GDBusMessage* reply = impl_->MethodCall(appid, method_name, serialized);
+  if (reply == nullptr)
+    return {};
+
+  auto reply_ptr = std::unique_ptr<GDBusMessage, decltype(g_object_unref)*>(
+      reply, g_object_unref);
+  GError* err = nullptr;
+  if (g_dbus_message_to_gerror(reply, &err)) {
+    LOGE("MethodCall() is failed. error(%s)", err->message);
+    if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
+      set_last_result(ERROR_PERMISSION_DENIED);
+    else
+      set_last_result(ERROR_IO_ERROR);
+
+    g_error_free(err);
+    return {};
+  }
+
   GVariant *reply_body = g_dbus_message_get_body(reply);
+  if (reply_body == nullptr) {
+    LOGE("g_dbus_message_get_body() is failed");
+    set_last_result(ERROR_OUT_OF_MEMORY);
+    return {};
+  }
 
   GVariantIter *iter = nullptr;
-  list<Bundle> ret_list;
   g_variant_get(reply_body, "(a(s))", &iter);
+  if (iter == nullptr) {
+    LOGE("Failed to get GVariantIter");
+    set_last_result(ERROR_OUT_OF_MEMORY);
+    return {};
+  }
+
+  list<Bundle> ret_list;
   char* raw = nullptr;
   while (g_variant_iter_loop(iter, "(&s)", &raw) && raw != nullptr) {
     Bundle ret(raw);
     ret_list.emplace_back(ret);
   }
 
-  if (iter)
-    g_variant_iter_free(iter);
-  if (reply)
-    g_object_unref(reply);
+  g_variant_iter_free(iter);
+  set_last_result(ERROR_NONE);
   return ret_list;
 }
 
@@ -170,12 +196,33 @@ int DBusSender::RequestNumber(const IEventInfo& info) {
   Bundle serialized = info.Serialize();
 
   GDBusMessage* reply = impl_->MethodCall(appid, method_name, serialized);
+  if (reply == nullptr)
+    return 0;
+
+  auto reply_ptr = std::unique_ptr<GDBusMessage, decltype(g_object_unref)*>(
+      reply, g_object_unref);
+  GError* err = nullptr;
+  if (g_dbus_message_to_gerror(reply, &err)) {
+    LOGE("MethodCall() is failed. error(%s)", err->message);
+    if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
+      set_last_result(ERROR_PERMISSION_DENIED);
+    else
+      set_last_result(ERROR_IO_ERROR);
+
+    g_error_free(err);
+    return 0;
+  }
+
   GVariant *reply_body = g_dbus_message_get_body(reply);
+  if (reply_body == nullptr) {
+    LOGE("g_dbus_message_get_body() is failed");
+    set_last_result(ERROR_OUT_OF_MEMORY);
+    return 0;
+  }
 
   int num;
   g_variant_get(reply_body, "(i)", &num);
-  if (reply)
-    g_object_unref(reply);
+  set_last_result(ERROR_NONE);
   return num;
 }