Skip to content

Commit 4a8446c

Browse files
committed
K64F EMAC: Fix TX error path leaks
1 parent 6d71ded commit 4a8446c

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

features/netsocket/emac-drivers/TARGET_Freescale/k64f_emac.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -410,36 +410,38 @@ void K64F_EMAC::packet_tx()
410410
*/
411411
bool K64F_EMAC::link_out(emac_mem_buf_t *buf)
412412
{
413-
emac_mem_buf_t *temp_pbuf;
414-
415413
// If buffer is chained or not aligned then make a contiguous aligned copy of it
416414
if (memory_manager->get_next(buf) ||
417415
reinterpret_cast<uint32_t>(memory_manager->get_ptr(buf)) % ENET_BUFF_ALIGNMENT) {
418-
temp_pbuf = memory_manager->alloc_heap(memory_manager->get_total_len(buf), ENET_BUFF_ALIGNMENT);
419-
if (NULL == temp_pbuf)
420-
return false;
416+
emac_mem_buf_t *copy_buf;
417+
copy_buf = memory_manager->alloc_heap(memory_manager->get_total_len(buf), ENET_BUFF_ALIGNMENT);
418+
if (NULL == copy_buf) {
419+
memory_manager->free(buf);
420+
return false;
421+
}
421422

422423
// Copy to new buffer and free original
423-
memory_manager->copy(temp_pbuf, buf);
424+
memory_manager->copy(copy_buf, buf);
424425
memory_manager->free(buf);
425-
} else {
426-
temp_pbuf = buf;
426+
buf = copy_buf;
427427
}
428428

429429
/* Check if a descriptor is available for the transfer. */
430-
if (xTXDCountSem.wait(0) == 0)
430+
if (xTXDCountSem.wait(0) == 0) {
431+
memory_manager->free(buf);
431432
return false;
433+
}
432434

433435
/* Get exclusive access */
434436
TXLockMutex.lock();
435437

436438
/* Save the buffer so that it can be freed when transmit is done */
437-
tx_buff[tx_produce_index % ENET_TX_RING_LEN] = temp_pbuf;
439+
tx_buff[tx_produce_index % ENET_TX_RING_LEN] = buf;
438440
tx_produce_index += 1;
439441

440442
/* Setup transfers */
441-
g_handle.txBdCurrent->buffer = static_cast<uint8_t *>(memory_manager->get_ptr(temp_pbuf));
442-
g_handle.txBdCurrent->length = memory_manager->get_len(temp_pbuf);
443+
g_handle.txBdCurrent->buffer = static_cast<uint8_t *>(memory_manager->get_ptr(buf));
444+
g_handle.txBdCurrent->length = memory_manager->get_len(buf);
443445
g_handle.txBdCurrent->control |= (ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK);
444446

445447
/* Increase the buffer descriptor address. */

0 commit comments

Comments
 (0)