Fix wrong implementation 70/236670/2
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 19 Jun 2020 07:31:45 +0000 (16:31 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 19 Jun 2020 07:41:39 +0000 (16:41 +0900)
The FdBroker ptr has to be removed when the FdBroker instance is removed.
The FdBroker.Cancel() method can be invoked from the Proxy instance directly.
The exceptions about the registration handle are added in the callback functions
related to gdbus.

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

index 54ed899..daf3090 100644 (file)
@@ -217,6 +217,7 @@ FdBroker::FdBroker(bool mock) : mock_(mock) {
 }
 
 FdBroker::~FdBroker() {
+  FdBrokerManager::GetInst().Remove(this);
   Cancel();
 }
 
@@ -224,8 +225,6 @@ void FdBroker::Cancel() {
   std::lock_guard<std::recursive_mutex> lock(mutex_);
   LOGI("FdBroker::Cancel!");
 
-  FdBrokerManager::GetInst().Remove(this);
-
   if (cancellable_) {
     LOGW("Cancel the send request");
     g_cancellable_cancel(cancellable_);
@@ -507,11 +506,16 @@ void FdBroker::OnReceiveDbusMethod(GDBusConnection *conn,
     gpointer user_data) {
   FdBroker* broker = static_cast<FdBroker*>(user_data);
   if (!FdBrokerManager::GetInst().Exist(broker)) {
-    LOGE("No such broker(%p)", broker);
+    LOGE("No such FdBroker(%p)", broker);
     return;
   }
 
   std::lock_guard<std::recursive_mutex> lock(broker->mutex_);
+  if (broker->registration_id_ == 0) {
+    LOGE("Invalid context. FdBroker(%p)", broker);
+    return;
+  }
+
   int ret = -1;
   char sender_appid[255];
   int sender_pid;
@@ -712,11 +716,16 @@ void FdBroker::OnNameAppeared(GDBusConnection *connection,
                               gpointer user_data) {
   FdBroker* broker = static_cast<FdBroker*>(user_data);
   if (!FdBrokerManager::GetInst().Exist(broker)) {
-    LOGE("No such broker(%p)", broker);
+    LOGE("No such FdBroker(%p)", broker);
     return;
   }
 
   std::lock_guard<std::recursive_mutex> lock(broker->mutex_);
+  if (broker->watcher_id_ == 0) {
+    LOGE("Invalid context. FdBroker(%p)", broker);
+    return;
+  }
+
   int pid = broker->GetSenderPid(connection, name_owner);
   char owner_appid[255] = { 0, };
 
@@ -741,11 +750,16 @@ void FdBroker::OnNameVanished(GDBusConnection *connection,
                               gpointer user_data) {
   FdBroker* broker = static_cast<FdBroker*>(user_data);
   if (!FdBrokerManager::GetInst().Exist(broker)) {
-    LOGE("No such broker(%p)", broker);
+    LOGE("No such FdBroker(%p)", broker);
     return;
   }
 
   std::lock_guard<std::recursive_mutex> lock(broker->mutex_);
+  if (broker->watcher_id_ == 0) {
+    LOGE("Invalid context. FdBroker(%p)", broker);
+    return;
+  }
+
   broker->watcher_->OnPortVanished(broker->watch_appid_,
       broker->watch_port_name_);
 }
@@ -842,6 +856,11 @@ void FdBroker::OnResultReceived(GObject* source_object,
   }
 
   std::lock_guard<std::recursive_mutex> lock(broker->mutex_);
+  if (broker->cancellable_ == nullptr) {
+    LOGE("Invalid context. Fdbroker(%p)", broker);
+    return;
+  }
+
   IEventWatcher* watcher = broker->watcher_;
 
   if (err) {