diff mbox series

[BlueZ,3/4] shared/bap: Fix crash when canceling requests

Message ID 20221109222947.1137901-3-luiz.dentz@gmail.com (mailing list archive)
State Accepted
Commit 7fcd6889fb13858e3f78e9d5e35ffd80d8e0accb
Headers show
Series [BlueZ,1/4] shared/util: Add iovec helpers | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
tedd_an/checkpatch success Checkpatch PASS
tedd_an/gitlint success Gitlint PASS

Commit Message

Luiz Augusto von Dentz Nov. 9, 2022, 10:29 p.m. UTC
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

If bt_bap_unref/bap_free is called while there is an ongoing pending
request it may endup calling into bap_notify_ready which will try to
notify ready callbacks while holding a reference, but in case the
reference is already 0 that means it would switch to 1 and back 0
causing a double free.

To prevent that bap_notify_ready now checks that the reference is not 0
with use of bt_bap_ref_safe.
---
 src/shared/bap.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/src/shared/bap.c b/src/shared/bap.c
index c3c0d596fe91..1bb982fd080a 100644
--- a/src/shared/bap.c
+++ b/src/shared/bap.c
@@ -2721,6 +2721,14 @@  struct bt_bap *bt_bap_ref(struct bt_bap *bap)
 	return bap;
 }
 
+static struct bt_bap *bt_bap_ref_safe(struct bt_bap *bap)
+{
+	if (!bap || !bap->ref_count)
+		return NULL;
+
+	return bt_bap_ref(bap);
+}
+
 void bt_bap_unref(struct bt_bap *bap)
 {
 	if (!bap)
@@ -2739,7 +2747,8 @@  static void bap_notify_ready(struct bt_bap *bap)
 	if (!queue_isempty(bap->pending))
 		return;
 
-	bt_bap_ref(bap);
+	if (!bt_bap_ref_safe(bap))
+		return;
 
 	for (entry = queue_get_entries(bap->ready_cbs); entry;
 							entry = entry->next) {