Skip to content

Commit 0034e12

Browse files
axelnxpkartben
authored andcommitted
drivers: bluetooth: hci_nxp: fix nomem handling
When allocation fails in `hci_rx_cb`, we should return immediately instead of continuing the execution of the function. Also, we need to free the allocated buffer from the heap when sending to the message queue fails to avoid memory leak. Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
1 parent 70f55bc commit 0034e12

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

drivers/bluetooth/hci/hci_nxp.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,19 +368,23 @@ K_THREAD_DEFINE(nxp_hci_rx_thread, CONFIG_BT_DRV_RX_STACK_SIZE, bt_rx_thread, NU
368368
static void hci_rx_cb(uint8_t packetType, uint8_t *data, uint16_t len)
369369
{
370370
struct hci_data hci_rx_frame;
371+
int ret;
371372

372373
hci_rx_frame.packetType = packetType;
373374
hci_rx_frame.data = k_malloc(len);
374375

375376
if (!hci_rx_frame.data) {
376377
LOG_ERR("Failed to allocate RX buffer");
378+
return;
377379
}
378380

379381
memcpy(hci_rx_frame.data, data, len);
380382
hci_rx_frame.len = len;
381383

382-
if (k_msgq_put(&rx_msgq, &hci_rx_frame, K_NO_WAIT) < 0) {
383-
LOG_ERR("Failed to push RX data to message queue");
384+
ret = k_msgq_put(&rx_msgq, &hci_rx_frame, K_NO_WAIT);
385+
if (ret < 0) {
386+
LOG_ERR("Failed to push RX data to message queue: %d", ret);
387+
k_free(hci_rx_frame.data);
384388
}
385389
}
386390

0 commit comments

Comments
 (0)