Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12534 fuzzing crash (stack overflow) in isConstStatement() #6164

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1824,7 +1824,7 @@ static bool isConstant(const Token* tok) {
return tok && (tok->isEnumerator() || Token::Match(tok, "%bool%|%num%|%str%|%char%|nullptr|NULL"));
}

static bool isConstStatement(const Token *tok)
static bool isConstStatement(const Token *tok, bool isNestedBracket = false)
{
if (!tok)
return false;
Expand Down Expand Up @@ -1875,9 +1875,13 @@ static bool isConstStatement(const Token *tok)
if (Token::simpleMatch(tok, "?") && Token::simpleMatch(tok->astOperand2(), ":")) // ternary operator
return isConstStatement(tok->astOperand1()) && isConstStatement(tok->astOperand2()->astOperand1()) && isConstStatement(tok->astOperand2()->astOperand2());
if (isBracketAccess(tok) && isWithoutSideEffects(tok->astOperand1(), /*checkArrayAccess*/ true, /*checkReference*/ false)) {
if (Token::simpleMatch(tok->astParent(), "["))
return isConstStatement(tok->astOperand2()) && isConstStatement(tok->astParent());
return isConstStatement(tok->astOperand2());
const bool isChained = succeeds(tok->astParent(), tok);
if (Token::simpleMatch(tok->astParent(), "[")) {
if (isChained)
return isConstStatement(tok->astOperand2()) && isConstStatement(tok->astParent());
return isNestedBracket && isConstStatement(tok->astOperand2());
}
return isConstStatement(tok->astOperand2(), /*isNestedBracket*/ !isChained);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d f(t*a){a[a[3]]}
7 changes: 7 additions & 0 deletions test/testincompletestatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,13 @@ class TestIncompleteStatement : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant code: Found a statement that begins with enumerator constant.\n",
errout_str());

check("void f(int* a) {\n" // #12534
" a[a[3]];\n"
" a[a[g()]];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (warning) Redundant code: Found unused array access.\n",
errout_str());
}

void vardecl() {
Expand Down
Loading