[M108 Migration][WRTjs]add protection for DBusConnection 93/289293/4
authorzhaosy <shiyusy.zhao@samsung.com>
Mon, 6 Mar 2023 02:47:33 +0000 (10:47 +0800)
committerBot Blink <blinkbot@samsung.com>
Mon, 6 Mar 2023 08:14:21 +0000 (08:14 +0000)
Add protection for DBusConnection for avoid some abnormal
crash happen.

Reference Patch:
https://review.tizen.org/gerrit/288901/

Change-Id: I42fd0770422bc4f826aa746e75b871d68efbaf3a
Signed-off-by: zhaosy <shiyusy.zhao@samsung.com>
wrt/src/app/service_main.cc
wrt/src/browser/wrt_ipc.cc

index 325662f..cf85223 100644 (file)
@@ -73,7 +73,6 @@ void ChildCreated() {
 
 void ChildTerminated(int sig_no) {
   LOG(INFO) << "sig_no : " << sig_no;
-  wrt_ipc->SendMessage("CloseDBusConnection", "");
   wrt_ipc.reset();
   child_created = false;
 
@@ -128,7 +127,7 @@ void OnAppControl(app_control_h app_control, void* data) {
     // not touch here.
     started_apps.erase(command_value);
   }
-  if (wrt_ipc->HasDbusOwner(kWrtDbusName)) {
+  if (wrt_ipc && wrt_ipc->HasDbusOwner(kWrtDbusName)) {
     wrt_ipc->SendMessage(command_key.c_str(), command_value.c_str());
   } else {
     command_queue.push(std::make_pair(command_key, command_value));
index 23a80f5..d74491f 100755 (executable)
@@ -19,10 +19,16 @@ const char* InterProcessCommunication::Message::Sender() const {
 }
 
 void InterProcessCommunication::Message::CloseDBusConnection() const {
-  dbus_connection_close(connection_);
+  if (connection_) {
+    dbus_connection_close(connection_);
+  }
 }
 
 void InterProcessCommunication::Message::Reply(const char* argument) const {
+  if (!connection_) {
+    LOG(ERROR) << "connection_ is not valid";
+    return;
+  }
   DBusMessage* reply = dbus_message_new_method_return(message_);
   dbus_message_append_args(reply, DBUS_TYPE_STRING, &argument, DBUS_TYPE_INVALID);
   dbus_connection_send(connection_, reply, nullptr);
@@ -54,8 +60,11 @@ InterProcessCommunication::InterProcessCommunication(bool is_daemon) {
 }
 
 InterProcessCommunication::~InterProcessCommunication() {
-  dbus_connection_close(connection_);
-  dbus_connection_unref(connection_);
+  if (connection_) {
+    dbus_connection_close(connection_);
+    dbus_connection_unref(connection_);
+    connection_ = nullptr;
+  }
 }
 
 // static
@@ -72,6 +81,10 @@ DBusHandlerResult InterProcessCommunication::OnMessage(DBusConnection* connectio
 
   std::string member = dbus_message_get_member(message);
   auto* ipc = reinterpret_cast<InterProcessCommunication*>(user_data);
+  if (!ipc || !ipc->connection_) {
+    LOG(ERROR) << "ipc or connection_ is not valid";
+    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+  }
   auto iterator = ipc->message_handlers_.find(member);
   if (iterator != ipc->message_handlers_.end()) {
     DBusError error;
@@ -110,6 +123,10 @@ void InterProcessCommunication::Listen() {
 }
 
 DBusMessage* InterProcessCommunication::InternalSendMessage(const char* receiver, const char* type, const char* argument, bool need_reply) {
+  if (!connection_) {
+    LOG(ERROR) << "connection_ is not valid";
+    return nullptr;
+  }
   DBusMessage* message = dbus_message_new_method_call(receiver, WRT_DBUS_PATH, WRT_DBUS_NAME, type);
   if (!message) {
     LOG(ERROR) << "Fail to create dbus message";
@@ -184,6 +201,11 @@ bool InterProcessCommunication::SendMessageAndWaitReply(const char* receiver, co
 }
 
 bool InterProcessCommunication::HasDbusOwner(const char* dbus_name) {
+  if (!connection_) {
+    LOG(ERROR) << "connection_ is not valid";
+    return false;
+  }
+
   DBusError err;
   dbus_error_init(&err);