From 0d33f58fe74e1ad4aa047ebfd0005afe580cfdb3 Mon Sep 17 00:00:00 2001 From: Ajay Panicker Date: Tue, 1 May 2018 17:29:07 -0700 Subject: [PATCH] Use weak pointers instead of base::Unretained to prevent use after free Bug: 78517259 Test: Compile and switch users while AVRCP is connected Change-Id: I9eb4482e4de52fed7750747afdc7cfb5402615a4 --- profile/avrcp/connection_handler.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/profile/avrcp/connection_handler.cc b/profile/avrcp/connection_handler.cc index 874bf772d..9ba8384e7 100644 --- a/profile/avrcp/connection_handler.cc +++ b/profile/avrcp/connection_handler.cc @@ -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 newDevice = std::make_shared( -- 2.11.0