Skip to content

Commit

Permalink
Ensure WebsocketConnection::send releases source stream on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Mar 27, 2024
1 parent 99b2ccd commit 337eaa8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDataSourceStream> sourceRef(source);

if(source == nullptr) {
return false;
}
Expand Down Expand Up @@ -279,11 +282,11 @@ bool WebsocketConnection::send(IDataSourceStream* source, ws_frame_type_t type,

// send the header
if(!connection->send(reinterpret_cast<const char*>(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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 337eaa8

Please sign in to comment.