From 145f972d76eaf3cd947a92789ecefb4cc1b78765 Mon Sep 17 00:00:00 2001 From: Alex Rebert Date: Sat, 30 Oct 2021 13:43:41 -0400 Subject: [PATCH] Fix out-of-bounds read in RemoveLineComments Follow up to 6f07e89fdfb, which was not sufficient to fix the bug. Fix https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=24553 --- code/Common/RemoveComments.cpp | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/code/Common/RemoveComments.cpp b/code/Common/RemoveComments.cpp index 9974e985a..2de9666de 100644 --- a/code/Common/RemoveComments.cpp +++ b/code/Common/RemoveComments.cpp @@ -65,27 +65,19 @@ void CommentRemover::RemoveLineComments(const char* szComment, len = lenBuffer; } - char *szCurrent = szBuffer; - while (*szCurrent) { - + for(size_t i = 0; i < lenBuffer; i++) { // skip over quotes - if (*szCurrent == '\"' || *szCurrent == '\'') - while (*szCurrent++ && *szCurrent != '\"' && *szCurrent != '\''); + if (szBuffer[i] == '\"' || szBuffer[i] == '\'') + while (++i < lenBuffer && szBuffer[i] != '\"' && szBuffer[i] != '\''); - size_t lenRemaining = lenBuffer - (szCurrent - szBuffer); - if(lenRemaining < len) { + if(lenBuffer - i < len) { break; } - if (!strncmp(szCurrent,szComment,len)) { - while (!IsLineEnd(*szCurrent)) - *szCurrent++ = chReplacement; - - if (!*szCurrent) { - break; - } + if (!strncmp(szBuffer + i,szComment,len)) { + while (i < lenBuffer && !IsLineEnd(szBuffer[i])) + szBuffer[i++] = chReplacement; } - ++szCurrent; } }