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:
parent
e3c98a03cc
commit
26897b2425
@ -15,6 +15,12 @@
|
||||
#include <cmath>
|
||||
|
||||
|
||||
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<WebSocketMessage>(WebSocketMessageType::Close,
|
||||
"",
|
||||
emptyMsg,
|
||||
wireSize,
|
||||
WebSocketErrorInfo(),
|
||||
WebSocketOpenInfo(),
|
||||
@ -217,7 +223,7 @@ namespace ix
|
||||
|
||||
_onMessageCallback(ix::make_unique<WebSocketMessage>(
|
||||
WebSocketMessageType::Open,
|
||||
"",
|
||||
emptyMsg,
|
||||
0,
|
||||
WebSocketErrorInfo(),
|
||||
WebSocketOpenInfo(status.uri, status.headers, status.protocol),
|
||||
@ -251,7 +257,7 @@ namespace ix
|
||||
|
||||
_onMessageCallback(
|
||||
ix::make_unique<WebSocketMessage>(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<WebSocketMessage>(WebSocketMessageType::Error,
|
||||
"",
|
||||
emptyMsg,
|
||||
0,
|
||||
connectErr,
|
||||
WebSocketOpenInfo(),
|
||||
|
@ -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>;
|
||||
|
Loading…
Reference in New Issue
Block a user