From 337eaa83f330c81289d853a96997921a8f29d69e Mon Sep 17 00:00:00 2001 From: mikee47 Date: Wed, 27 Mar 2024 15:30:36 +0000 Subject: [PATCH] Ensure `WebsocketConnection::send` releases source stream on failure --- .../src/Network/Http/Websocket/WebsocketConnection.cpp | 7 +++++-- .../src/Network/Http/Websocket/WebsocketConnection.h | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp index 81d1c46cb4..cde8eef270 100644 --- a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp +++ b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.cpp @@ -194,6 +194,9 @@ bool WebsocketConnection::send(const char* message, size_t length, ws_frame_type bool WebsocketConnection::send(IDataSourceStream* source, ws_frame_type_t type, bool useMask, bool isFin) { + // Ensure source gets destroyed if we return prematurely + std::unique_ptr sourceRef(source); + if(source == nullptr) { return false; } @@ -279,11 +282,11 @@ bool WebsocketConnection::send(IDataSourceStream* source, ws_frame_type_t type, // send the header if(!connection->send(reinterpret_cast(packet), packetLength)) { - delete source; return false; } - return connection->send(source); + // Pass stream to connection + return connection->send(sourceRef.release()); } void WebsocketConnection::broadcast(const char* message, size_t length, ws_frame_type_t type) diff --git a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h index fc5b32350f..799bed25f0 100644 --- a/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h +++ b/Sming/Components/Network/src/Network/Http/Websocket/WebsocketConnection.h @@ -109,14 +109,14 @@ class WebsocketConnection /** * @brief Sends websocket message from a stream - * @param stream + * @param source The stream to send - we get ownership of the stream * @param type * @param useMask MUST be true for client connections * @param isFin true if this is the final frame * * @retval bool true on success */ - bool send(IDataSourceStream* stream, ws_frame_type_t type = WS_FRAME_TEXT, bool useMask = false, bool isFin = true); + bool send(IDataSourceStream* source, ws_frame_type_t type = WS_FRAME_TEXT, bool useMask = false, bool isFin = true); /** * @brief Broadcasts a message to all active websocket connections