Skip to content

Commit 3f81e81

Browse files
lylezhu2012kartben
authored andcommitted
Bluetooth: AVDTP: Fix memory leak issue
In function `avdtp_send`, there is case that if the session->req is not NULL, then the buf will be lost. Release the allocated buffer by buffer allocated function when the buf is failed to send. Signed-off-by: Lyle Zhu <[email protected]>
1 parent 9e21615 commit 3f81e81

File tree

1 file changed

+28
-5
lines changed
  • subsys/bluetooth/host/classic

1 file changed

+28
-5
lines changed

subsys/bluetooth/host/classic/avdtp.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,6 @@ static int avdtp_send(struct bt_avdtp *session, struct net_buf *buf, struct bt_a
986986
result = bt_l2cap_chan_send(&session->br_chan.chan, buf);
987987
if (result < 0) {
988988
LOG_ERR("Error:L2CAP send fail - result = %d", result);
989-
net_buf_unref(buf);
990989
bt_avdtp_clear_req(session);
991990
return result;
992991
}
@@ -1306,6 +1305,7 @@ int bt_avdtp_init(void)
13061305
int bt_avdtp_discover(struct bt_avdtp *session, struct bt_avdtp_discover_params *param)
13071306
{
13081307
struct net_buf *buf;
1308+
int err;
13091309

13101310
LOG_DBG("");
13111311
if (!param || !session) {
@@ -1319,7 +1319,12 @@ int bt_avdtp_discover(struct bt_avdtp *session, struct bt_avdtp_discover_params
13191319
return -ENOMEM;
13201320
}
13211321

1322-
return avdtp_send(session, buf, &param->req);
1322+
err = avdtp_send(session, buf, &param->req);
1323+
if (err) {
1324+
net_buf_unref(buf);
1325+
}
1326+
1327+
return err;
13231328
}
13241329

13251330
int bt_avdtp_parse_sep(struct net_buf *buf, struct bt_avdtp_sep_info *sep_info)
@@ -1345,6 +1350,7 @@ int bt_avdtp_get_capabilities(struct bt_avdtp *session,
13451350
struct bt_avdtp_get_capabilities_params *param)
13461351
{
13471352
struct net_buf *buf;
1353+
int err;
13481354

13491355
LOG_DBG("");
13501356
if (!param || !session) {
@@ -1362,7 +1368,12 @@ int bt_avdtp_get_capabilities(struct bt_avdtp *session,
13621368
/* Body of the message */
13631369
net_buf_add_u8(buf, (param->stream_endpoint_id << 2U));
13641370

1365-
return avdtp_send(session, buf, &param->req);
1371+
err = avdtp_send(session, buf, &param->req);
1372+
if (err) {
1373+
net_buf_unref(buf);
1374+
}
1375+
1376+
return err;
13661377
}
13671378

13681379
int bt_avdtp_parse_capability_codec(struct net_buf *buf, uint8_t *codec_type,
@@ -1433,6 +1444,7 @@ static int avdtp_process_configure_command(struct bt_avdtp *session, uint8_t cmd
14331444
struct bt_avdtp_set_configuration_params *param)
14341445
{
14351446
struct net_buf *buf;
1447+
int err;
14361448

14371449
LOG_DBG("");
14381450
if (!param || !session) {
@@ -1466,7 +1478,12 @@ static int avdtp_process_configure_command(struct bt_avdtp *session, uint8_t cmd
14661478
/* Codec Info Element */
14671479
net_buf_add_mem(buf, param->codec_specific_ie, param->codec_specific_ie_len);
14681480

1469-
return avdtp_send(session, buf, &param->req);
1481+
err = avdtp_send(session, buf, &param->req);
1482+
if (err) {
1483+
net_buf_unref(buf);
1484+
}
1485+
1486+
return err;
14701487
}
14711488

14721489
int bt_avdtp_set_configuration(struct bt_avdtp *session,
@@ -1502,6 +1519,7 @@ static int bt_avdtp_ctrl(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *
15021519
uint8_t check_state)
15031520
{
15041521
struct net_buf *buf;
1522+
int err;
15051523

15061524
LOG_DBG("");
15071525
if (!param || !session || !param->sep) {
@@ -1523,7 +1541,12 @@ static int bt_avdtp_ctrl(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *
15231541
/* ACP Stream Endpoint ID */
15241542
net_buf_add_u8(buf, (param->acp_stream_ep_id << 2U));
15251543

1526-
return avdtp_send(session, buf, &param->req);
1544+
err = avdtp_send(session, buf, &param->req);
1545+
if (err) {
1546+
net_buf_unref(buf);
1547+
}
1548+
1549+
return err;
15271550
}
15281551

15291552
int bt_avdtp_open(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param)

0 commit comments

Comments
 (0)