From 4f5b0c4f07b8fd7ecacef3eb97a00072a6426fc9 Mon Sep 17 00:00:00 2001 From: Nikos Athanasiou Date: Mon, 7 Jun 2021 21:19:52 +0300 Subject: [PATCH] Noexcept ix web socket per message deflate options (#299) * Fix unsafe calls and safeguard WebSocketMessage from being called w/ temporaries * Use unnamed namespace to express internal linkage * Avoid returning references that are mutex protected Motivation for this MR The antipattern of returning references to mutex protected members was removed. Since a caller can hold the reference it would make all class level locking meaningless. Instead values are returned. The IXWebSocketPerMessageDeflateOptions class was shrunk by 7 bytes (1 padding + 2*3) after changing the int members to the used uint8_t; side effects of that were handled. An inefficient "string -> int" was replaced by standard library. As seen here http://coliru.stacked-crooked.com/a/46b5990bafb9c626 this gives an order of magnitude better performance. * noexcept string to integer conversion --- ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp b/ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp index 63d72173..c41a8c3d 100644 --- a/ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp +++ b/ixwebsocket/IXWebSocketPerMessageDeflateOptions.cpp @@ -85,7 +85,7 @@ namespace ix if (startsWith(token, "server_max_window_bits=")) { - uint8_t x = std::stoi(token.substr(token.find_last_of("=") + 1)); + uint8_t x = strtol(token.substr(token.find_last_of("=") + 1).c_str(), nullptr, 10); // Sanitize values to be in the proper range [8, 15] in // case a server would give us bogus values @@ -95,7 +95,7 @@ namespace ix if (startsWith(token, "client_max_window_bits=")) { - uint8_t x = std::stoi(token.substr(token.find_last_of("=") + 1)); + uint8_t x = strtol(token.substr(token.find_last_of("=") + 1).c_str(), nullptr, 10); // Sanitize values to be in the proper range [8, 15] in // case a server would give us bogus values