Skip to content

UdpContext - check after pbuf_alloc for errors #3354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions libraries/ESP8266WiFi/src/include/UdpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ class UdpContext
{
_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;
while(left_to_copy)
Expand All @@ -271,17 +276,25 @@ class UdpContext
{
size_t data_size = _tx_buf_offset;
pbuf* tx_copy = pbuf_alloc(PBUF_TRANSPORT, data_size, PBUF_RAM);
uint8_t* dst = reinterpret_cast<uint8_t*>(tx_copy->payload);
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;
if(!tx_copy){
DEBUGV("failed pbuf_alloc");
}
else{
uint8_t* dst = reinterpret_cast<uint8_t*>(tx_copy->payload);
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);
_tx_buf_head = 0;
_tx_buf_cur = 0;
_tx_buf_offset = 0;
if(!tx_copy){
return false;
}


if (!addr) {
Expand Down Expand Up @@ -313,6 +326,10 @@ class UdpContext
if (!_tx_buf_head)
{
_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_offset = 0;
}
Expand All @@ -326,6 +343,10 @@ class UdpContext
while(grow_size)
{
pbuf* pb = pbuf_alloc(PBUF_TRANSPORT, pbuf_unit_size, PBUF_RAM);
if (!pb)
{
return;
}
pbuf_cat(_tx_buf_head, pb);
if (grow_size < pbuf_unit_size)
return;
Expand Down