Skip to content

Commit 8434b01

Browse files
moonlight83340cfriedt
authored andcommitted
drivers: ethernet: add check for null fragment before TX loop
In eth_cyclonev_send(), add a guard to detect if the net_pkt has no data buffer (i.e., pkt->buffer is NULL) before starting the TX descriptor loop. This prevents a potential null pointer dereference on frag->data in the first iteration of the do-while loop. The previous in-loop check for frag was redundant and misleading: it still allowed access to frag->data even when frag could be NULL, making the code both unnecessary and potentially unsafe. The new check ensures frag is valid before entering the loop, covering the rare case where net_pkt has no associated buffer. Signed-off-by: Gaetan Perrot <[email protected]>
1 parent 447e118 commit 8434b01

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

drivers/ethernet/eth_cyclonev.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ static int eth_cyclonev_send(const struct device *dev, struct net_pkt *pkt)
408408

409409
LOG_DBG("Pkt length: %d", len);
410410
frag = pkt->buffer;
411+
412+
__ASSERT((frag != NULL), "Invalid net_pkt: no data buffer\n");
413+
411414
do {
412415

413416
/* reserve a free descriptor for this fragment */
@@ -433,10 +436,7 @@ static int eth_cyclonev_send(const struct device *dev, struct net_pkt *pkt)
433436

434437
/* Copy data to local buffer */
435438

436-
if (frag) {
437-
memcpy(&p->tx_buf[p->tx_current_desc_number * ETH_BUFFER_SIZE], frag->data,
438-
len);
439-
}
439+
memcpy(&p->tx_buf[p->tx_current_desc_number * ETH_BUFFER_SIZE], frag->data, len);
440440

441441
/* Set the buffer size. */
442442
tx_desc->control_buffer_size = (frag->len & ETH_DMATXDESC_TBS1);

0 commit comments

Comments
 (0)