Skip to content

Commit 3e6c25c

Browse files
authored
udp: limit buffer depth (#6895)
This commit avoids OOMs on an udp corner case where a delay() in the main loop would allow memory filling. A memory leak has been observed with such semantically forbidden delay, unsolved yet, and preventing to use a simple counter instead of walking through a linked list. The count limit is however small.
1 parent 70c3370 commit 3e6c25c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

libraries/ESP8266WiFi/src/include/UdpContext.h

+18
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ class UdpContext
265265
// ref'ing it to prevent release from the below pbuf_free(deleteme)
266266
pbuf_ref(_rx_buf);
267267
}
268+
// remove the already-consumed head of the chain
268269
pbuf_free(deleteme);
269270

270271
_rx_buf_offset = 0;
@@ -440,6 +441,19 @@ class UdpContext
440441
const ip_addr_t *srcaddr, u16_t srcport)
441442
{
442443
(void) upcb;
444+
// check receive pbuf chain depth
445+
{
446+
pbuf* p;
447+
int count = 0;
448+
for (p = _rx_buf; p && ++count < rxBufMaxDepth*2; p = p->next);
449+
if (p)
450+
{
451+
// pbuf chain too deep, dropping
452+
pbuf_free(pb);
453+
DEBUGV(":udr\r\n");
454+
return;
455+
}
456+
}
443457

444458
#if LWIP_VERSION_MAJOR == 1
445459
#define TEMPDSTADDR (&current_iphdr_dest)
@@ -531,6 +545,10 @@ class UdpContext
531545
srcaddr(src), dstaddr(dst), srcport(srcport) { }
532546
};
533547
AddrHelper _currentAddr;
548+
549+
// rx pbuf depth barrier (counter of buffered UDP received packets)
550+
// keep it small
551+
static constexpr int rxBufMaxDepth = 4;
534552
};
535553

536554

0 commit comments

Comments
 (0)