OSDN Git Service

Use weak pointers instead of base::Unretained to prevent use after free
authorAjay Panicker <apanicke@google.com>
Wed, 2 May 2018 00:29:07 +0000 (17:29 -0700)
committerAjay Panicker <apanicke@google.com>
Wed, 2 May 2018 23:46:37 +0000 (23:46 +0000)
Bug: 78517259
Test: Compile and switch users while AVRCP is connected
Change-Id: I9eb4482e4de52fed7750747afdc7cfb5402615a4

profile/avrcp/connection_handler.cc

index 874bf77..9ba8384 100644 (file)
@@ -160,8 +160,9 @@ bool ConnectionHandler::SdpLookup(const RawAddress& bdaddr, SdpCallback cb) {
 
   return avrc_->FindService(
              UUID_SERVCLASS_AV_REMOTE_CONTROL, bdaddr, &db_params,
-             base::Bind(&ConnectionHandler::SdpCb, base::Unretained(this),
-                        bdaddr, cb, disc_db)) == AVRC_SUCCESS;
+             base::Bind(&ConnectionHandler::SdpCb,
+                        weak_ptr_factory_.GetWeakPtr(), bdaddr, cb, disc_db)) ==
+         AVRC_SUCCESS;
 }
 
 bool ConnectionHandler::AvrcpConnect(bool initiator, const RawAddress& bdaddr) {
@@ -176,7 +177,7 @@ bool ConnectionHandler::AvrcpConnect(bool initiator, const RawAddress& bdaddr) {
                                     weak_ptr_factory_.GetWeakPtr());
   }
   open_cb.msg_cback =
-      base::Bind(&ConnectionHandler::MessageCb, base::Unretained(this));
+      base::Bind(&ConnectionHandler::MessageCb, weak_ptr_factory_.GetWeakPtr());
   open_cb.company_id = AVRC_CO_GOOGLE;
   open_cb.conn = initiator ? AVRC_CONN_INT
                            : AVRC_CONN_ACP;  // 0 if initiator, 1 if acceptor
@@ -286,7 +287,7 @@ void ConnectionHandler::AcceptorControlCb(uint8_t handle, uint8_t event,
       LOG(INFO) << __PRETTY_FUNCTION__ << ": Connection Opened Event";
 
       auto&& callback = base::Bind(&ConnectionHandler::SendMessage,
-                                   base::Unretained(this), handle);
+                                   weak_ptr_factory_.GetWeakPtr(), handle);
       auto&& ctrl_mtu = avrc_->GetPeerMtu(handle) - AVCT_HDR_LEN;
       auto&& browse_mtu = avrc_->GetBrowseMtu(handle) - AVCT_HDR_LEN;
       std::shared_ptr<Device> newDevice = std::make_shared<Device>(