From 83527c69d3f69ba2b049b827c4f80c0633dce390 Mon Sep 17 00:00:00 2001 From: Jelle De Vleeschouwer Date: Tue, 22 Oct 2019 22:35:42 +0200 Subject: [PATCH] Resolved memory leak Cherry-picked 81cde92b3c12cc4f753dc8fefd4801e1abaa2e7c from development. --- modules/pico_ethernet.c | 9 +++++---- stack/pico_device.c | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/modules/pico_ethernet.c b/modules/pico_ethernet.c index cc86e8562..301072b7f 100644 --- a/modules/pico_ethernet.c +++ b/modules/pico_ethernet.c @@ -295,7 +295,7 @@ static int pico_ethernet_ipv6_dst(struct pico_frame *f, struct pico_eth *const d /* Ethernet send, first attempt: try our own address. * Returns 0 if the packet is not for us. - * Returns 1 if the packet is cloned to our own receive queue, so the caller can discard the original frame. + * Returns 1 if the packet is cloned to our own receive queue and the original frame is dicarded. * */ static int32_t pico_ethsend_local(struct pico_frame *f, struct pico_eth_hdr *hdr) { @@ -308,7 +308,9 @@ static int32_t pico_ethsend_local(struct pico_frame *f, struct pico_eth_hdr *hdr dbg("sending out packet destined for our own mac\n"); if (pico_ethernet_receive(clone) < 0) { dbg("pico_ethernet_receive() failed\n"); + return 0; } + pico_frame_discard(f); return 1; } @@ -317,13 +319,12 @@ static int32_t pico_ethsend_local(struct pico_frame *f, struct pico_eth_hdr *hdr /* Ethernet send, second attempt: try bcast. * Returns 0 if the packet is not bcast, so it will be handled somewhere else. - * Returns 1 if the packet is handled by the pico_device_broadcast() function, so it can be discarded. + * Returns 1 if the packet is handled by the pico_device_broadcast() function and is discarded. * */ static int32_t pico_ethsend_bcast(struct pico_frame *f) { if (IS_LIMITED_BCAST(f)) { - (void)pico_device_broadcast(f); /* We can discard broadcast even if it's not sent. */ - return 1; + return (pico_device_broadcast(f) > 0); // Return 1 on success, ret > 0 } return 0; diff --git a/stack/pico_device.c b/stack/pico_device.c index 4d06f1a74..6be33bfb5 100644 --- a/stack/pico_device.c +++ b/stack/pico_device.c @@ -450,6 +450,7 @@ int32_t pico_device_broadcast(struct pico_frame *f) { struct pico_tree_node *index; int32_t ret = -1; + int sent = 0; pico_tree_foreach(index, &Device_tree) { @@ -462,14 +463,29 @@ int32_t pico_device_broadcast(struct pico_frame *f) break; copy->dev = dev; - copy->dev->send(copy->dev, copy->start, (int)copy->len); + ret = copy->dev->send(copy->dev, copy->start, (int)copy->len); + /* FIXME: If a device driver returns zero (which means the device + * driver is currently busy) there is no means to retry the + * broadcast operation later. */ pico_frame_discard(copy); } else { ret = f->dev->send(f->dev, f->start, (int)f->len); + /* FIXME: If a device driver returns zero (which means the device + * driver is currently busy) there is no means to retry the + * broadcast operation later. */ + } + + /* FIXME: If at least one device driver was able to sent the frame on + * the wire, the broadcast operation will be considered successful. */ + if (ret > 0) { + sent = 1; } } + + ret = sent ? f->len : -1; + pico_frame_discard(f); return ret; }