From d5cf17ff652132b52430635e24b963edf8595154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Villac=C3=ADs=20Lasso?= Date: Tue, 16 Feb 2021 19:54:43 -0500 Subject: [PATCH] Clear write queue BEFORE calling discard callback The discard callback (installed via onDisconnect()) can legitimately decide to destroy the AsyncTCP client, calling the destructors which in turn invalidate the write mutex object. Prior to this commit, calling _clearWriteQueue() (which takes said write mutex) risked accessing uninitialized memory and causing memory corruption. Fixed. --- src/AsyncTCP.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index 5f9bae0..77fe1d9 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -718,9 +718,9 @@ void AsyncClient::_close(void) _socket = -1; xSemaphoreGive(_asyncsock_mutex); + _clearWriteQueue(); if (_discard_cb) _discard_cb(_discard_cb_arg, this); _removeAllCallbacks(); - _clearWriteQueue(); } void AsyncClient::_error(int8_t err) @@ -731,10 +731,10 @@ void AsyncClient::_error(int8_t err) _socket = -1; xSemaphoreGive(_asyncsock_mutex); + _clearWriteQueue(); if (_error_cb) _error_cb(_error_cb_arg, this, err); if (_discard_cb) _discard_cb(_discard_cb_arg, this); _removeAllCallbacks(); - _clearWriteQueue(); } size_t AsyncClient::space()