From 59cab0833f9df54e02869d677581e4ef8e399830 Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Wed, 26 Jun 2024 20:43:16 -0400 Subject: [PATCH] NETstring: Fix when maxlen is 0 --- lib/netplay/nettypes.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/netplay/nettypes.cpp b/lib/netplay/nettypes.cpp index 2ec1659d6a2..c50a647ce6a 100644 --- a/lib/netplay/nettypes.cpp +++ b/lib/netplay/nettypes.cpp @@ -901,10 +901,11 @@ void NETstring(char *str, uint16_t maxlen) queueAuto(len); // Truncate length if necessary - if (len > maxlen - 1) + uint16_t maxReadLen = (maxlen > 0) ? static_cast(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) @@ -912,7 +913,7 @@ void NETstring(char *str, uint16_t maxlen) queueAuto(str[i]); } - if (NETgetPacketDir() == PACKET_DECODE) + if (NETgetPacketDir() == PACKET_DECODE && maxlen > 0) { // NUL-terminate str[len] = '\0';