Skip to content

Commit

Permalink
shared/bap: Fix crash when canceling requests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Vudentz committed Nov 15, 2022
1 parent 918c73a commit 7fcd688
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/shared/bap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2638,6 +2638,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)
Expand All @@ -2656,7 +2664,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) {
Expand Down

0 comments on commit 7fcd688

Please sign in to comment.