1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-06 05:21:22 +03:00

UdpContext: check that pbuf_alloc doesn't return nullptr (#3354)

This commit is contained in:
Matthew Blythe 2017-09-22 02:56:27 -06:00 committed by Ivan Grokhotkov
parent 369edb616d
commit 13c1e8b293

View File

@ -246,6 +246,11 @@ public:
{ {
_reserve(_tx_buf_offset + size); _reserve(_tx_buf_offset + size);
} }
if (!_tx_buf_head || _tx_buf_head->tot_len < _tx_buf_offset + size)
{
DEBUGV("failed _reserve");
return 0;
}
size_t left_to_copy = size; size_t left_to_copy = size;
while(left_to_copy) while(left_to_copy)
@ -271,17 +276,25 @@ public:
{ {
size_t data_size = _tx_buf_offset; size_t data_size = _tx_buf_offset;
pbuf* tx_copy = pbuf_alloc(PBUF_TRANSPORT, data_size, PBUF_RAM); pbuf* tx_copy = pbuf_alloc(PBUF_TRANSPORT, data_size, PBUF_RAM);
uint8_t* dst = reinterpret_cast<uint8_t*>(tx_copy->payload); if(!tx_copy){
for (pbuf* p = _tx_buf_head; p; p = p->next) { DEBUGV("failed pbuf_alloc");
size_t will_copy = (data_size < p->len) ? data_size : p->len; }
memcpy(dst, p->payload, will_copy); else{
dst += will_copy; uint8_t* dst = reinterpret_cast<uint8_t*>(tx_copy->payload);
data_size -= will_copy; for (pbuf* p = _tx_buf_head; p; p = p->next) {
size_t will_copy = (data_size < p->len) ? data_size : p->len;
memcpy(dst, p->payload, will_copy);
dst += will_copy;
data_size -= will_copy;
}
} }
pbuf_free(_tx_buf_head); pbuf_free(_tx_buf_head);
_tx_buf_head = 0; _tx_buf_head = 0;
_tx_buf_cur = 0; _tx_buf_cur = 0;
_tx_buf_offset = 0; _tx_buf_offset = 0;
if(!tx_copy){
return false;
}
if (!addr) { if (!addr) {
@ -313,6 +326,10 @@ private:
if (!_tx_buf_head) if (!_tx_buf_head)
{ {
_tx_buf_head = pbuf_alloc(PBUF_TRANSPORT, pbuf_unit_size, PBUF_RAM); _tx_buf_head = pbuf_alloc(PBUF_TRANSPORT, pbuf_unit_size, PBUF_RAM);
if (!_tx_buf_head)
{
return;
}
_tx_buf_cur = _tx_buf_head; _tx_buf_cur = _tx_buf_head;
_tx_buf_offset = 0; _tx_buf_offset = 0;
} }
@ -326,6 +343,10 @@ private:
while(grow_size) while(grow_size)
{ {
pbuf* pb = pbuf_alloc(PBUF_TRANSPORT, pbuf_unit_size, PBUF_RAM); pbuf* pb = pbuf_alloc(PBUF_TRANSPORT, pbuf_unit_size, PBUF_RAM);
if (!pb)
{
return;
}
pbuf_cat(_tx_buf_head, pb); pbuf_cat(_tx_buf_head, pb);
if (grow_size < pbuf_unit_size) if (grow_size < pbuf_unit_size)
return; return;