Skip to content

Commit

Permalink
Avoid strnlen
Browse files Browse the repository at this point in the history
`strnlen` is not available on some platforms, such as macOS 10.4.
  • Loading branch information
glebm committed Nov 15, 2024
1 parent a838078 commit 31c28e7
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Source/dvlnet/base_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,16 @@ tl::expected<void, PacketError> base_protocol<P>::recv_decrypted(packet &pkt, en
return {};
std::vector<std::string> playerNames;
for (size_t i = 0; i < Players.size(); i++) {
std::string playerName;
const char *playerNamePointer = reinterpret_cast<const char *>(infoBuffer.data() + sizeof(GameData) + (i * PlayerNameLength));
playerName.append(playerNamePointer, strnlen(playerNamePointer, PlayerNameLength));
if (!playerName.empty())
playerNames.push_back(playerName);
std::string_view playerNameBuffer {
reinterpret_cast<const char *>(infoBuffer.data() + sizeof(GameData) + (i * PlayerNameLength)),
PlayerNameLength
};
if (const size_t nullPos = playerNameBuffer.find('\0'); nullPos != std::string_view::npos) {
playerNameBuffer.remove_suffix(playerNameBuffer.size() - nullPos);
}
if (!playerNameBuffer.empty()) {
playerNames.emplace_back(playerNameBuffer);
}
}
std::string gameName;
size_t gameNameSize = infoBuffer.size() - neededSize;
Expand Down

0 comments on commit 31c28e7

Please sign in to comment.