Skip to content

Commit 93932ad

Browse files
Fix #13197 Crash in isExhaustiveSwitch() (#6875)
1 parent 1855f5c commit 93932ad

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

lib/tokenlist.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ static bool isPrefixUnary(const Token* tok, bool cpp)
913913
}
914914
}
915915
if (!tok->previous()
916-
|| ((Token::Match(tok->previous(), "(|[|{|%op%|;|?|:|,|.|return|::") || (cpp && tok->strAt(-1) == "throw"))
916+
|| ((Token::Match(tok->previous(), "(|[|{|%op%|;|?|:|,|.|case|return|::") || (cpp && tok->strAt(-1) == "throw"))
917917
&& (tok->previous()->tokType() != Token::eIncDecOp || tok->tokType() == Token::eIncDecOp)))
918918
return true;
919919

@@ -1893,6 +1893,11 @@ void TokenList::validateAst(bool print) const
18931893
"' doesn't have two operands.",
18941894
InternalError::AST);
18951895
}
1896+
if (tok->str() == "case" && !tok->astOperand1()) {
1897+
throw InternalError(tok,
1898+
"Syntax Error: AST broken, 'case' doesn't have an operand.",
1899+
InternalError::AST);
1900+
}
18961901

18971902
// Check member access
18981903
if (Token::Match(tok, "%var% .")) {

test/testtokenize.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6903,6 +6903,7 @@ class TestTokenizer : public TestFixture {
69036903
ASSERT_EQUALS("12+case", testAst("case 1+2:"));
69046904
ASSERT_EQUALS("xyz:?case", testAst("case (x?y:z):"));
69056905
ASSERT_EQUALS("switchx( 1case y++ 2case", testAst("switch(x){case 1:{++y;break;case 2:break;}}"));
6906+
ASSERT_EQUALS("switchi( 12<<~case 0return", testAst("switch (i) { case ~(1 << 2) : return 0; }")); // #13197
69066907
}
69076908

69086909
void astrefqualifier() {

0 commit comments

Comments
 (0)