From 26897b242598194c233eec9ebcdfecc080c47e0b Mon Sep 17 00:00:00 2001 From: Nikos Athanasiou Date: Fri, 4 Jun 2021 04:39:38 +0300 Subject: [PATCH] Fix unsafe calls and safeguard WebSocketMessage (#294) * Fix unsafe calls and safeguard WebSocketMessage from being called w/ temporaries * Use unnamed namespace to express internal linkage --- ixwebsocket/IXWebSocket.cpp | 14 ++++++++++---- ixwebsocket/IXWebSocketMessage.h | 12 ++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index e0815ad3..1675533e 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -15,6 +15,12 @@ #include +namespace +{ + const std::string emptyMsg; +} // namespace + + namespace ix { OnTrafficTrackerCallback WebSocket::_onTrafficTrackerCallback = nullptr; @@ -38,7 +44,7 @@ namespace ix [this](uint16_t code, const std::string& reason, size_t wireSize, bool remote) { _onMessageCallback( ix::make_unique(WebSocketMessageType::Close, - "", + emptyMsg, wireSize, WebSocketErrorInfo(), WebSocketOpenInfo(), @@ -217,7 +223,7 @@ namespace ix _onMessageCallback(ix::make_unique( WebSocketMessageType::Open, - "", + emptyMsg, 0, WebSocketErrorInfo(), WebSocketOpenInfo(status.uri, status.headers, status.protocol), @@ -251,7 +257,7 @@ namespace ix _onMessageCallback( ix::make_unique(WebSocketMessageType::Open, - "", + emptyMsg, 0, WebSocketErrorInfo(), WebSocketOpenInfo(status.uri, status.headers), @@ -338,7 +344,7 @@ namespace ix connectErr.http_status = status.http_status; _onMessageCallback(ix::make_unique(WebSocketMessageType::Error, - "", + emptyMsg, 0, connectErr, WebSocketOpenInfo(), diff --git a/ixwebsocket/IXWebSocketMessage.h b/ixwebsocket/IXWebSocketMessage.h index ae07d38c..25a00ce7 100644 --- a/ixwebsocket/IXWebSocketMessage.h +++ b/ixwebsocket/IXWebSocketMessage.h @@ -42,6 +42,18 @@ namespace ix { ; } + + /** + * @brief Deleted overload to prevent binding `str` to a temporary, which would cause + * undefined behavior since class members don't extend lifetime beyond the constructor call. + */ + WebSocketMessage(WebSocketMessageType t, + std::string&& s, + size_t w, + WebSocketErrorInfo e, + WebSocketOpenInfo o, + WebSocketCloseInfo c, + bool b = false) = delete; }; using WebSocketMessagePtr = std::unique_ptr;