Skip to content

Commit 66f77f9

Browse files
Vudentznashif
authored andcommitted
Bluetooth: ATT: Fix crash if bt_l2cap_send_cb fails
This fixes a regression introduced by 10841b9 as it did remove a call to net_buf_ref which was used not only to keep a reference for resending but also to prevent bt_l2cap_send_cb to unref the buffer in case it fails. Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent 9e3c7b9 commit 66f77f9

File tree

1 file changed

+12
-0
lines changed
  • subsys/bluetooth/host

1 file changed

+12
-0
lines changed

subsys/bluetooth/host/att.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,23 @@ static int chan_send(struct bt_att_chan *chan, struct net_buf *buf,
189189

190190
chan->sent = cb ? cb : chan_cb(buf);
191191

192+
/* bt_l2cap_send_cb takes onwership of the buffer so take another
193+
* reference to restore the state in case an error is returned.
194+
*/
195+
net_buf_ref(buf);
196+
192197
err = bt_l2cap_send_cb(chan->att->conn, BT_L2CAP_CID_ATT,
193198
buf, att_cb(chan->sent),
194199
&chan->chan.chan);
195200
if (err) {
201+
/* In case of an error has occurred restore the buffer state as
202+
* the extra reference shall have prevented the buffer to be
203+
* freed.
204+
*/
196205
net_buf_simple_restore(&buf->b, &state);
206+
} else {
207+
/* In case of success unref the extra reference taken */
208+
net_buf_unref(buf);
197209
}
198210

199211
return err;

0 commit comments

Comments
 (0)