Skip to content

Commit bb57cb0

Browse files
committed
fix(ws): Fix handling of allocation failures
H/t @mathieucarbou
1 parent 68c995e commit bb57cb0

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/AsyncWebSocket.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ void AsyncWebSocketClient::close(uint16_t code, const char *message) {
448448
if (c) {
449449
c->abort();
450450
}
451+
return;
451452
}
452453
}
453454
_queueControl(WS_DISCONNECT);
@@ -593,7 +594,9 @@ void AsyncWebSocketClient::_onData(void *pbuf, size_t plen) {
593594
"[%s][%" PRIu32 "] DATA processing next fragment of %s frame %" PRIu32 ", index: %" PRIu64 ", len: %" PRIu32 "", _server->url(), _clientId,
594595
(_pinfo.message_opcode == WS_TEXT) ? "text" : "binary", _pinfo.num, _pinfo.index, (uint32_t)datalen
595596
);
596-
_handleDataEvent(data, datalen, datalen == plen); // datalen == plen means that we are processing the last part of the current TCP packet
597+
if (!_handleDataEvent(data, datalen, datalen == plen)) { // datalen == plen means that we are processing the last part of the current TCP packet
598+
return; // stop processing on failure
599+
}
597600
}
598601

599602
// track index for next fragment
@@ -642,7 +645,9 @@ void AsyncWebSocketClient::_onData(void *pbuf, size_t plen) {
642645
(_pinfo.message_opcode == WS_TEXT) ? "text" : "binary", _pinfo.num, _pinfo.index, (uint32_t)datalen
643646
);
644647

645-
_handleDataEvent(data, datalen, datalen == plen); // datalen == plen means that we are processing the last part of the current TCP packet
648+
if (!_handleDataEvent(data, datalen, datalen == plen)) { // datalen == plen means that we are processing the last part of the current TCP packet
649+
return; // stop processing on failure
650+
}
646651

647652
if (_pinfo.final) {
648653
_pinfo.num = 0;
@@ -672,7 +677,7 @@ void AsyncWebSocketClient::_onData(void *pbuf, size_t plen) {
672677
}
673678
}
674679

675-
void AsyncWebSocketClient::_handleDataEvent(uint8_t *data, size_t len, bool endOfPaquet) {
680+
bool AsyncWebSocketClient::_handleDataEvent(uint8_t *data, size_t len, bool endOfPaquet) {
676681
// ------------------------------------------------------------
677682
// Issue 384: https://github.com/ESP32Async/ESPAsyncWebServer/issues/384
678683
// Discussion: https://github.com/ESP32Async/ESPAsyncWebServer/pull/383#discussion_r2760425739
@@ -703,9 +708,11 @@ void AsyncWebSocketClient::_handleDataEvent(uint8_t *data, size_t len, bool endO
703708
_server->_handleEvent(this, WS_EVT_DATA, (void *)&_pinfo, copy.get(), len);
704709
} else {
705710
async_ws_log_e("Failed to allocate");
706-
if (_client) {
707-
_client->abort();
711+
AsyncClient *c = _client;
712+
if (c) {
713+
c->abort();
708714
}
715+
return false; // failure!
709716
}
710717
} else {
711718
uint8_t backup = data[len];
@@ -716,6 +723,7 @@ void AsyncWebSocketClient::_handleDataEvent(uint8_t *data, size_t len, bool endO
716723
} else {
717724
_server->_handleEvent(this, WS_EVT_DATA, (void *)&_pinfo, data, len);
718725
}
726+
return true;
719727
}
720728

721729
size_t AsyncWebSocketClient::printf(const char *format, ...) {

src/AsyncWebSocket.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ class AsyncWebSocketClient {
190190
void _runQueue(asyncsrv::unique_lock_type &lock);
191191

192192
// this function is called when a text message is received, in order to copy the buffer and place a null terminator at the end of the buffer for easier handling of text messages.
193-
void _handleDataEvent(uint8_t *data, size_t len, bool endOfPaquet);
193+
// Returns true on success, false on failure (e.g. memory allocation failure)
194+
bool _handleDataEvent(uint8_t *data, size_t len, bool endOfPaquet);
194195

195196
public:
196197
void *_tempObject;

0 commit comments

Comments
 (0)