From 057a2c801a40c2f280193da9963b009adb9f75f6 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 23 Sep 2024 15:44:44 +0200 Subject: [PATCH] Tier1: exclude delimiter from split string A string "test3,test4" using the delimiter ',' would be split into "test3," and "test4", but should be "test3" and "test4". The delimiter should not be included. This patch fixes the issue. --- src/tier1/strtools.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/tier1/strtools.cpp b/src/tier1/strtools.cpp index 454eb186..73730739 100644 --- a/src/tier1/strtools.cpp +++ b/src/tier1/strtools.cpp @@ -446,7 +446,15 @@ void V_SplitString2(const char* pString, const char** pSeparators, ssize_t nSepa if (pFirstSeparator > pCurPos) { - outStrings.AddToTail(AllocString(pCurPos, pFirstSeparator - pCurPos)); + const ssize_t nLen = (pFirstSeparator-1) - pCurPos; + char* const pSplit = AllocString(pCurPos, nLen); + + // We need to terminate the array here since we copy the string + // from the list minus the delimiter, and AllocString assumes + // the null is already in the source buffer (also reserving + // space for it). therefore we need len+1 to terminate it. + pSplit[nLen + 1] = '\0'; + outStrings.AddToTail(pSplit); } pCurPos = pFirstSeparator + separatorLen;