From 9b89ad2fa9bb8b3d2f9a626078199c6fb795109d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awek=20Piotrowski?= Date: Sat, 6 Oct 2018 09:29:30 +0200 Subject: [PATCH] Fix hard-fault when socket created using accept() is closed When socket created using accept() is closed by calling the close() method, "delete this" is executed which triggers the destructor call on TCPSocket which in turn calls close() once again. Because _stack is already 0 this results in a hard-fault. Add a check that skips the rest of the close() method is the _stack is already 0. --- .../features/netsocket/InternetSocket/test_InternetSocket.cpp | 1 + features/netsocket/InternetSocket.cpp | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp b/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp index 3b499fef42..c71bebc264 100644 --- a/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp +++ b/UNITTESTS/features/netsocket/InternetSocket/test_InternetSocket.cpp @@ -204,6 +204,7 @@ TEST_F(TestInternetSocket, getsockopt) TEST_F(TestInternetSocket, sigio) { callback_is_called = false; + socket->open((NetworkStack *)&stack); socket->sigio(mbed::callback(my_callback)); socket->close(); // Trigger event; EXPECT_EQ(callback_is_called, true); diff --git a/features/netsocket/InternetSocket.cpp b/features/netsocket/InternetSocket.cpp index 948e25374a..cc793450a2 100644 --- a/features/netsocket/InternetSocket.cpp +++ b/features/netsocket/InternetSocket.cpp @@ -56,6 +56,10 @@ nsapi_error_t InternetSocket::close() _lock.lock(); nsapi_error_t ret = NSAPI_ERROR_OK; + if (!_stack) { + return ret; + } + if (_socket) { _stack->socket_attach(_socket, 0, 0); nsapi_socket_t socket = _socket;