Skip to content

Commit

Permalink
NETstring: Fix when maxlen is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Jun 27, 2024
1 parent 55216d0 commit 59cab08
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/netplay/nettypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,18 +901,19 @@ void NETstring(char *str, uint16_t maxlen)
queueAuto(len);

// Truncate length if necessary
if (len > maxlen - 1)
uint16_t maxReadLen = (maxlen > 0) ? static_cast<uint16_t>(maxlen - 1) : 0;
if (len > maxReadLen)
{
debug(LOG_ERROR, "NETstring: %s packet, length %u truncated at %u", NETgetPacketDir() == PACKET_ENCODE ? "Encoding" : "Decoding", len, maxlen);
len = maxlen - 1;
len = maxReadLen;
}

for (unsigned i = 0; i < len; ++i)
{
queueAuto(str[i]);
}

if (NETgetPacketDir() == PACKET_DECODE)
if (NETgetPacketDir() == PACKET_DECODE && maxlen > 0)
{
// NUL-terminate
str[len] = '\0';
Expand Down

0 comments on commit 59cab08

Please sign in to comment.