Skip to content

Commit

Permalink
Fix #13197 Crash in isExhaustiveSwitch() (#6875)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Oct 7, 2024
1 parent 1855f5c commit 93932ad
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ static bool isPrefixUnary(const Token* tok, bool cpp)
}
}
if (!tok->previous()
|| ((Token::Match(tok->previous(), "(|[|{|%op%|;|?|:|,|.|return|::") || (cpp && tok->strAt(-1) == "throw"))
|| ((Token::Match(tok->previous(), "(|[|{|%op%|;|?|:|,|.|case|return|::") || (cpp && tok->strAt(-1) == "throw"))
&& (tok->previous()->tokType() != Token::eIncDecOp || tok->tokType() == Token::eIncDecOp)))
return true;

Expand Down Expand Up @@ -1893,6 +1893,11 @@ void TokenList::validateAst(bool print) const
"' doesn't have two operands.",
InternalError::AST);
}
if (tok->str() == "case" && !tok->astOperand1()) {
throw InternalError(tok,
"Syntax Error: AST broken, 'case' doesn't have an operand.",
InternalError::AST);
}

// Check member access
if (Token::Match(tok, "%var% .")) {
Expand Down
1 change: 1 addition & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6903,6 +6903,7 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS("12+case", testAst("case 1+2:"));
ASSERT_EQUALS("xyz:?case", testAst("case (x?y:z):"));
ASSERT_EQUALS("switchx( 1case y++ 2case", testAst("switch(x){case 1:{++y;break;case 2:break;}}"));
ASSERT_EQUALS("switchi( 12<<~case 0return", testAst("switch (i) { case ~(1 << 2) : return 0; }")); // #13197
}

void astrefqualifier() {
Expand Down

0 comments on commit 93932ad

Please sign in to comment.