From 165e37bad69dd758512630d2910ddafca475bfe2 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Fri, 15 Nov 2024 11:29:21 +0000 Subject: [PATCH] Avoid `strnlen` `strnlen` is not available on some platforms, such as macOS 10.4. --- Source/dvlnet/base_protocol.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Source/dvlnet/base_protocol.h b/Source/dvlnet/base_protocol.h index 76bf524a2f0..a3b91e62c51 100644 --- a/Source/dvlnet/base_protocol.h +++ b/Source/dvlnet/base_protocol.h @@ -9,6 +9,7 @@ #include "dvlnet/packet.h" #include "player.h" #include "utils/log.hpp" +#include "utils/stdcompat/string_view.hpp" namespace devilution { namespace net { @@ -300,11 +301,16 @@ void base_protocol

::recv_decrypted(packet &pkt, endpoint_t sender) return; std::vector playerNames; for (size_t i = 0; i < Players.size(); i++) { - std::string playerName; - const char *playerNamePointer = (const char *)(pkt.Info().data() + sizeof(GameData) + (i * PlayerNameLength)); - playerName.append(playerNamePointer, strnlen(playerNamePointer, PlayerNameLength)); - if (!playerName.empty()) - playerNames.push_back(playerName); + string_view playerNameBuffer { + reinterpret_cast(pkt.Info().data() + sizeof(GameData) + (i * PlayerNameLength)), + PlayerNameLength + }; + if (const size_t nullPos = playerNameBuffer.find('\0'); nullPos != string_view::npos) { + playerNameBuffer.remove_suffix(playerNameBuffer.size() - nullPos); + } + if (!playerNameBuffer.empty()) { + playerNames.emplace_back(playerNameBuffer.data(), playerNameBuffer.size()); + } } std::string gameName; size_t gameNameSize = pkt.Info().size() - neededSize;