From d7ae3a3076af726c2cc2be1185de367cc10df2d9 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Wed, 18 Dec 2024 12:18:14 +0800 Subject: [PATCH] Replace magic numbers with compile time string length The ``sizeof()`` macro is evaluated at compile time. The string passed to it does not end up in the final binary and there is also no counting happening at runtime. The ``sizeof()`` operator counts the null terminator too. So ``sizeof("foo")`` is 4 and not 3. But this comes in handy because we want to skip over the length of the command and the slash character in front of it. --- src/game/server/gamecontext.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 7a03e2fb7d4..86ce39add39 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -2179,25 +2179,25 @@ void CGameContext::OnSayNetMessage(const CNetMsg_Cl_Say *pMsg, int ClientId, con if(str_startswith_nocase(pMsg->m_pMessage + 1, "w ")) { char aWhisperMsg[256]; - str_copy(aWhisperMsg, pMsg->m_pMessage + 3); + str_copy(aWhisperMsg, pMsg->m_pMessage + sizeof("w ")); Whisper(pPlayer->GetCid(), aWhisperMsg); } else if(str_startswith_nocase(pMsg->m_pMessage + 1, "whisper ")) { char aWhisperMsg[256]; - str_copy(aWhisperMsg, pMsg->m_pMessage + 9); + str_copy(aWhisperMsg, pMsg->m_pMessage + sizeof("whisper ")); Whisper(pPlayer->GetCid(), aWhisperMsg); } else if(str_startswith_nocase(pMsg->m_pMessage + 1, "c ")) { char aWhisperMsg[256]; - str_copy(aWhisperMsg, pMsg->m_pMessage + 3); + str_copy(aWhisperMsg, pMsg->m_pMessage + sizeof("c ")); Converse(pPlayer->GetCid(), aWhisperMsg); } else if(str_startswith_nocase(pMsg->m_pMessage + 1, "converse ")) { char aWhisperMsg[256]; - str_copy(aWhisperMsg, pMsg->m_pMessage + 10); + str_copy(aWhisperMsg, pMsg->m_pMessage + sizeof("converse ")); Converse(pPlayer->GetCid(), aWhisperMsg); } else