Improved emac rx task code to suppress Coverity false positive memory leak indication

pull/12720/head
Ondrej 2022-11-24 12:37:09 +00:00 zatwierdzone przez Ondrej Kosta
rodzic 9374a2d9ac
commit 409c7e5610
4 zmienionych plików z 71 dodań i 79 usunięć

Wyświetl plik

@ -805,32 +805,30 @@ static void emac_dm9051_task(void *arg)
uint32_t frame_len = ETH_MAX_PACKET_SIZE; uint32_t frame_len = ETH_MAX_PACKET_SIZE;
uint8_t *buffer; uint8_t *buffer;
dm9051_alloc_recv_buf(emac, &buffer, &frame_len); dm9051_alloc_recv_buf(emac, &buffer, &frame_len);
/* there is a waiting frame */ /* we have memory to receive the frame of maximal size previously defined */
if (frame_len) { if (buffer != NULL) {
/* we have memory to receive the frame of maximal size previously defined */ uint32_t buf_len = DM9051_ETH_MAC_RX_BUF_SIZE_AUTO;
if (buffer != NULL) { if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) {
uint32_t buf_len = DM9051_ETH_MAC_RX_BUF_SIZE_AUTO; if (buf_len == 0) {
if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) {
if (buf_len == 0) {
dm9051_flush_recv_frame(emac);
free(buffer);
} else if (frame_len > buf_len) {
ESP_LOGE(TAG, "received frame was truncated");
free(buffer);
} else {
ESP_LOGD(TAG, "receive len=%u", buf_len);
/* pass the buffer to stack (e.g. TCP/IP layer) */
emac->eth->stack_input(emac->eth, buffer, buf_len);
}
} else {
ESP_LOGE(TAG, "frame read from module failed");
dm9051_flush_recv_frame(emac); dm9051_flush_recv_frame(emac);
free(buffer); free(buffer);
} else if (frame_len > buf_len) {
ESP_LOGE(TAG, "received frame was truncated");
free(buffer);
} else {
ESP_LOGD(TAG, "receive len=%u", buf_len);
/* pass the buffer to stack (e.g. TCP/IP layer) */
emac->eth->stack_input(emac->eth, buffer, buf_len);
} }
} else { } else {
ESP_LOGE(TAG, "no mem for receive buffer"); ESP_LOGE(TAG, "frame read from module failed");
dm9051_flush_recv_frame(emac); dm9051_flush_recv_frame(emac);
free(buffer);
} }
/* if allocation failed and there is a waiting frame */
} else if (frame_len) {
ESP_LOGE(TAG, "no mem for receive buffer");
dm9051_flush_recv_frame(emac);
} }
} while (emac->packets_remain); } while (emac->packets_remain);
} }

Wyświetl plik

@ -259,28 +259,26 @@ static void emac_esp32_rx_task(void *arg)
/* set max expected frame len */ /* set max expected frame len */
uint32_t frame_len = ETH_MAX_PACKET_SIZE; uint32_t frame_len = ETH_MAX_PACKET_SIZE;
buffer = emac_hal_alloc_recv_buf(&emac->hal, &frame_len); buffer = emac_hal_alloc_recv_buf(&emac->hal, &frame_len);
/* there is a waiting frame */ /* we have memory to receive the frame of maximal size previously defined */
if (frame_len) { if (buffer != NULL) {
/* we have memory to receive the frame of maximal size previously defined */ uint32_t recv_len = emac_hal_receive_frame(&emac->hal, buffer, EMAC_HAL_BUF_SIZE_AUTO, &emac->frames_remain, &emac->free_rx_descriptor);
if (buffer != NULL) { if (recv_len == 0) {
uint32_t recv_len = emac_hal_receive_frame(&emac->hal, buffer, EMAC_HAL_BUF_SIZE_AUTO, &emac->frames_remain, &emac->free_rx_descriptor); ESP_LOGE(TAG, "frame copy error");
if (recv_len == 0) { free(buffer);
ESP_LOGE(TAG, "frame copy error");
free(buffer);
/* ensure that interface to EMAC does not get stuck with unprocessed frames */
emac_hal_flush_recv_frame(&emac->hal, &emac->frames_remain, &emac->free_rx_descriptor);
} else if (frame_len > recv_len) {
ESP_LOGE(TAG, "received frame was truncated");
free(buffer);
} else {
ESP_LOGD(TAG, "receive len= %d", recv_len);
emac->eth->stack_input(emac->eth, buffer, recv_len);
}
} else {
ESP_LOGE(TAG, "no mem for receive buffer");
/* ensure that interface to EMAC does not get stuck with unprocessed frames */ /* ensure that interface to EMAC does not get stuck with unprocessed frames */
emac_hal_flush_recv_frame(&emac->hal, &emac->frames_remain, &emac->free_rx_descriptor); emac_hal_flush_recv_frame(&emac->hal, &emac->frames_remain, &emac->free_rx_descriptor);
} else if (frame_len > recv_len) {
ESP_LOGE(TAG, "received frame was truncated");
free(buffer);
} else {
ESP_LOGD(TAG, "receive len= %d", recv_len);
emac->eth->stack_input(emac->eth, buffer, recv_len);
} }
/* if allocation failed and there is a waiting frame */
} else if (frame_len) {
ESP_LOGE(TAG, "no mem for receive buffer");
/* ensure that interface to EMAC does not get stuck with unprocessed frames */
emac_hal_flush_recv_frame(&emac->hal, &emac->frames_remain, &emac->free_rx_descriptor);
} }
#if CONFIG_ETH_SOFT_FLOW_CONTROL #if CONFIG_ETH_SOFT_FLOW_CONTROL
// we need to do extra checking of remained frames in case there are no unhandled frames left, but pause frame is still undergoing // we need to do extra checking of remained frames in case there are no unhandled frames left, but pause frame is still undergoing

Wyświetl plik

@ -691,32 +691,30 @@ static void emac_ksz8851snl_task(void *arg)
uint32_t frame_len = ETH_MAX_PACKET_SIZE; uint32_t frame_len = ETH_MAX_PACKET_SIZE;
uint8_t *buffer; uint8_t *buffer;
emac_ksz8851_alloc_recv_buf(emac, &buffer, &frame_len); emac_ksz8851_alloc_recv_buf(emac, &buffer, &frame_len);
/* there is a waiting frame */ /* we have memory to receive the frame of maximal size previously defined */
if (frame_len) { if (buffer != NULL) {
/* we have memory to receive the frame of maximal size previously defined */ uint32_t buf_len = KSZ8851_ETH_MAC_RX_BUF_SIZE_AUTO;
if (buffer != NULL) { if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) {
uint32_t buf_len = KSZ8851_ETH_MAC_RX_BUF_SIZE_AUTO; if (buf_len == 0) {
if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) {
if (buf_len == 0) {
emac_ksz8851_flush_recv_queue(emac);
free(buffer);
} else if (frame_len > buf_len) {
ESP_LOGE(TAG, "received frame was truncated");
free(buffer);
} else {
ESP_LOGD(TAG, "receive len=%u", buf_len);
/* pass the buffer to stack (e.g. TCP/IP layer) */
emac->eth->stack_input(emac->eth, buffer, buf_len);
}
} else {
ESP_LOGE(TAG, "frame read from module failed");
emac_ksz8851_flush_recv_queue(emac); emac_ksz8851_flush_recv_queue(emac);
free(buffer); free(buffer);
} else if (frame_len > buf_len) {
ESP_LOGE(TAG, "received frame was truncated");
free(buffer);
} else {
ESP_LOGD(TAG, "receive len=%u", buf_len);
/* pass the buffer to stack (e.g. TCP/IP layer) */
emac->eth->stack_input(emac->eth, buffer, buf_len);
} }
} else { } else {
ESP_LOGE(TAG, "no mem for receive buffer"); ESP_LOGE(TAG, "frame read from module failed");
emac_ksz8851_flush_recv_queue(emac); emac_ksz8851_flush_recv_queue(emac);
free(buffer);
} }
/* if allocation failed and there is a waiting frame */
} else if (frame_len) {
ESP_LOGE(TAG, "no mem for receive buffer");
emac_ksz8851_flush_recv_queue(emac);
} }
} }
ksz8851_write_reg(emac, KSZ8851_IER, ier); ksz8851_write_reg(emac, KSZ8851_IER, ier);

Wyświetl plik

@ -664,30 +664,28 @@ static void emac_w5500_task(void *arg)
/* define max expected frame len */ /* define max expected frame len */
frame_len = ETH_MAX_PACKET_SIZE; frame_len = ETH_MAX_PACKET_SIZE;
emac_w5500_alloc_recv_buf(emac, &buffer, &frame_len); emac_w5500_alloc_recv_buf(emac, &buffer, &frame_len);
/* there is a waiting frame */ /* we have memory to receive the frame of maximal size previously defined */
if (frame_len) { if (buffer != NULL) {
/* we have memory to receive the frame of maximal size previously defined */ buf_len = W5500_ETH_MAC_RX_BUF_SIZE_AUTO;
if (buffer != NULL) { if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) {
buf_len = W5500_ETH_MAC_RX_BUF_SIZE_AUTO; if (buf_len == 0) {
if (emac->parent.receive(&emac->parent, buffer, &buf_len) == ESP_OK) {
if (buf_len == 0) {
free(buffer);
} else if (frame_len > buf_len) {
ESP_LOGE(TAG, "received frame was truncated");
free(buffer);
} else {
ESP_LOGD(TAG, "receive len=%u", buf_len);
/* pass the buffer to stack (e.g. TCP/IP layer) */
emac->eth->stack_input(emac->eth, buffer, buf_len);
}
} else {
ESP_LOGE(TAG, "frame read from module failed");
free(buffer); free(buffer);
} else if (frame_len > buf_len) {
ESP_LOGE(TAG, "received frame was truncated");
free(buffer);
} else {
ESP_LOGD(TAG, "receive len=%u", buf_len);
/* pass the buffer to stack (e.g. TCP/IP layer) */
emac->eth->stack_input(emac->eth, buffer, buf_len);
} }
} else { } else {
ESP_LOGE(TAG, "no mem for receive buffer"); ESP_LOGE(TAG, "frame read from module failed");
emac_w5500_flush_recv_frame(emac); free(buffer);
} }
/* if allocation failed and there is a waiting frame */
} else if (frame_len) {
ESP_LOGE(TAG, "no mem for receive buffer");
emac_w5500_flush_recv_frame(emac);
} }
} while (emac->packets_remain); } while (emac->packets_remain);
} }