Skip to content

Commit

Permalink
Prevent bad casts, add AST validation
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 13, 2024
1 parent ea23882 commit a9ca283
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ static bool iscast(const Token *tok, bool cpp)
if (Token::simpleMatch(tok->link(), ") ( )"))
return false;

if (Token::Match(tok->link(), ") %assign%"))
if (Token::Match(tok->link(), ") %assign%|,|..."))
return false;

if (tok->previous() && tok->previous()->isName() && tok->previous()->str() != "return" &&
Expand Down Expand Up @@ -1819,9 +1819,13 @@ void TokenList::validateAst(bool print) const
tok = tok->link();
continue;
}
if (tok->isCast() && tok->astOperand1() && tok->link()) { // skip casts (not part of the AST)
tok = tok->link();
continue;
if (tok->isCast()) {
if (!tok->astOperand2() && precedes(tok->astOperand1(), tok))
throw InternalError(tok, "AST broken: '" + tok->str() + "' has improper operand.", InternalError::AST);
if (tok->astOperand1() && tok->link()) { // skip casts (not part of the AST)
tok = tok->link();
continue;
}
}

if (findLambdaEndToken(tok)) { // skip lambda captures
Expand Down
2 changes: 1 addition & 1 deletion test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6603,7 +6603,7 @@ class TestTokenizer : public TestFixture {

ASSERT_EQUALS("xfts(=", testAst("; auto x = f(ts...);"));

ASSERT_EQUALS("da((new= ifd(", testAst("template <typename a, typename... b>\n" // #10199
ASSERT_EQUALS("dae(new= ifd(", testAst("template <typename a, typename... b>\n" // #10199
"void c(b... e) {\n"
" a d = new a((e)...);\n"
" if (d) {}\n"
Expand Down

0 comments on commit a9ca283

Please sign in to comment.