Skip to content

Commit

Permalink
Fix double decrement of buffer size variable in localization code
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVanheer committed Feb 21, 2024
1 parent a3aeea4 commit d2da2f1
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions cl_dll/text_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ char* CHudTextMessage::LocaliseTextString(const char* msg, char* dst_buffer, int
// Subtract one so we have space for the null terminator no matter what.
std::size_t remainingBufferSize = buffer_size - 1;

for (const char* src = msg; *src != '\0' && remainingBufferSize > 0; --remainingBufferSize)
for (const char* src = msg; *src != '\0' && remainingBufferSize > 0;)
{
if (*src == '#')
{
Expand Down Expand Up @@ -90,29 +90,26 @@ char* CHudTextMessage::LocaliseTextString(const char* msg, char* dst_buffer, int

// lookup msg name in titles.txt
client_textmessage_t* clmsg = TextMessageGet(word_buf);
if (!clmsg || !(clmsg->pMessage))
if (clmsg && clmsg->pMessage)
{
src = word_start;
*dst = *src;
dst++;
src++;
// copy string into message over the msg name
const std::size_t count = std::min(remainingBufferSize, std::strlen(clmsg->pMessage));

std::strncpy(dst, clmsg->pMessage, count);

dst += count;
remainingBufferSize -= count;
continue;
}

// copy string into message over the msg name
const std::size_t count = std::min(remainingBufferSize, std::strlen(clmsg->pMessage));
src = word_start;
}

std::strncpy(dst, clmsg->pMessage, count);
*dst = *src;
dst++;
src++;

dst += count;
remainingBufferSize -= count;
}
else
{
*dst = *src;
dst++;
src++;
}
--remainingBufferSize;
}

*dst = '\0'; // ensure null termination
Expand Down

0 comments on commit d2da2f1

Please sign in to comment.