Skip to content

Commit

Permalink
Bluetooth: Host: Fix bt_l2cap_chan_ops.recv -EINPROGRESS
Browse files Browse the repository at this point in the history
Fix discrepancy in reference management between calls to
`bt_l2cap_chan_ops.recv` when the application returns `-EINPROGRESS`.

There are two call sites, `l2cap_chan_le_recv_sdu` and
`l2cap_chan_le_recv`, that were inconsistent.

`l2cap_chan_le_recv_sdu` moves the reference, and this patch updates
`l2cap_chan_le_recv_sdu` to do the same.

This behavior is also now documented.

This bug has existed since the introduction of this feature in
3151d26.

Signed-off-by: Aleksander Wasaznik <[email protected]>
  • Loading branch information
alwa-nordic committed Aug 8, 2024
1 parent 706ba43 commit 9f2dc36
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/zephyr/bluetooth/l2cap.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ struct bt_l2cap_chan_ops {
* @param chan The channel receiving data.
* @param buf Buffer containing incoming data.
*
* If the application returns -EINPROGRESS, the reference
* @p buf was moved.
*
* @return 0 in case of success or negative value in case of error.
* @return -EINPROGRESS in case where user has to confirm once the data
* has been processed by calling
Expand Down
11 changes: 10 additions & 1 deletion subsys/bluetooth/host/l2cap.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/math_extras.h>
#include <zephyr/sys/util.h>
#include <zephyr/net/buf.h>

#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/bluetooth.h>
Expand Down Expand Up @@ -2538,6 +2539,7 @@ static void l2cap_chan_le_recv_seg_direct(struct bt_l2cap_le_chan *chan, struct
static void l2cap_chan_le_recv(struct bt_l2cap_le_chan *chan,
struct net_buf *buf)
{
struct net_buf *owned_ref;
uint16_t sdu_len;
int err;

Expand Down Expand Up @@ -2611,7 +2613,14 @@ static void l2cap_chan_le_recv(struct bt_l2cap_le_chan *chan,
return;
}

err = chan->chan.ops->recv(&chan->chan, buf);
owned_ref = net_buf_ref(buf);
err = chan->chan.ops->recv(&chan->chan, owned_ref);
if (err != -EINPROGRESS) {
net_buf_unref(owned_ref);
owned_ref = NULL;
}


if (err < 0) {
if (err != -EINPROGRESS) {
LOG_ERR("err %d", err);
Expand Down

0 comments on commit 9f2dc36

Please sign in to comment.