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
This commit is contained in:
Nikos Athanasiou 2021-06-04 04:39:38 +03:00 committed by GitHub
parent e3c98a03cc
commit 26897b2425
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View File

@ -15,6 +15,12 @@
#include <cmath> #include <cmath>
namespace
{
const std::string emptyMsg;
} // namespace
namespace ix namespace ix
{ {
OnTrafficTrackerCallback WebSocket::_onTrafficTrackerCallback = nullptr; OnTrafficTrackerCallback WebSocket::_onTrafficTrackerCallback = nullptr;
@ -38,7 +44,7 @@ namespace ix
[this](uint16_t code, const std::string& reason, size_t wireSize, bool remote) { [this](uint16_t code, const std::string& reason, size_t wireSize, bool remote) {
_onMessageCallback( _onMessageCallback(
ix::make_unique<WebSocketMessage>(WebSocketMessageType::Close, ix::make_unique<WebSocketMessage>(WebSocketMessageType::Close,
"", emptyMsg,
wireSize, wireSize,
WebSocketErrorInfo(), WebSocketErrorInfo(),
WebSocketOpenInfo(), WebSocketOpenInfo(),
@ -217,7 +223,7 @@ namespace ix
_onMessageCallback(ix::make_unique<WebSocketMessage>( _onMessageCallback(ix::make_unique<WebSocketMessage>(
WebSocketMessageType::Open, WebSocketMessageType::Open,
"", emptyMsg,
0, 0,
WebSocketErrorInfo(), WebSocketErrorInfo(),
WebSocketOpenInfo(status.uri, status.headers, status.protocol), WebSocketOpenInfo(status.uri, status.headers, status.protocol),
@ -251,7 +257,7 @@ namespace ix
_onMessageCallback( _onMessageCallback(
ix::make_unique<WebSocketMessage>(WebSocketMessageType::Open, ix::make_unique<WebSocketMessage>(WebSocketMessageType::Open,
"", emptyMsg,
0, 0,
WebSocketErrorInfo(), WebSocketErrorInfo(),
WebSocketOpenInfo(status.uri, status.headers), WebSocketOpenInfo(status.uri, status.headers),
@ -338,7 +344,7 @@ namespace ix
connectErr.http_status = status.http_status; connectErr.http_status = status.http_status;
_onMessageCallback(ix::make_unique<WebSocketMessage>(WebSocketMessageType::Error, _onMessageCallback(ix::make_unique<WebSocketMessage>(WebSocketMessageType::Error,
"", emptyMsg,
0, 0,
connectErr, connectErr,
WebSocketOpenInfo(), WebSocketOpenInfo(),

View File

@ -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<WebSocketMessage>; using WebSocketMessagePtr = std::unique_ptr<WebSocketMessage>;