Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid strnlen (for 1.5) #7539

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Source/dvlnet/base_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -300,11 +301,16 @@ void base_protocol<P>::recv_decrypted(packet &pkt, endpoint_t sender)
return;
std::vector<std::string> 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<const char *>(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;
Expand Down
Loading