Fix crash 9492: Crash in FwdAnalysis::checkRecursive() (condTok is nullptr) (#2411)

* Fix crash 9492: Crash in FwdAnalysis::checkRecursive() (condTok is nullptr)

* Formatting
This commit is contained in:
Paul Fultz II 2019-12-01 07:53:03 -06:00 committed by Daniel Marjamäki
parent c1770414ba
commit f77347d7b4
4 changed files with 31 additions and 5 deletions

View File

@ -2473,7 +2473,7 @@ void Tokenizer::combineOperators()
} else if (tok->str() == "->") {
// If the preceding sequence is "( & %name% )", replace it by "%name%"
Token *t = tok->tokAt(-4);
if (Token::Match(t, "( & %name% )")) {
if (Token::Match(t, "( & %name% )") && !Token::simpleMatch(t->previous(), ">")) {
t->deleteThis();
t->deleteThis();
t->deleteNext();
@ -9471,7 +9471,7 @@ void Tokenizer::findGarbageCode() const
// Code must end with } ; ) NAME
if (!Token::Match(list.back(), "%name%|;|}|)"))
syntaxError(list.back());
if (list.back()->str() == ")" && !Token::Match(list.back()->link()->previous(), "%name% ("))
if (list.back()->str() == ")" && !Token::Match(list.back()->link()->previous(), "%name%|> ("))
syntaxError(list.back());
for (const Token *end = list.back(); end && end->isName(); end = end->previous()) {
if (Token::Match(end, "void|char|short|int|long|float|double|const|volatile|static|inline|struct|class|enum|union|template|sizeof|case|break|continue|typedef"))

View File

@ -1461,6 +1461,15 @@ void TokenList::validateAst() const
if (!tok->astOperand1() || !tok->astOperand2())
throw InternalError(tok, "Syntax Error: AST broken, binary operator '" + tok->str() + "' doesn't have two operands.", InternalError::AST);
}
// Check control blocks
if (Token::Match(tok->previous(), "if|while|for|switch (")) {
if (!tok->astOperand1() || !tok->astOperand2())
throw InternalError(tok,
"Syntax Error: AST broken, '" + tok->previous()->str() +
"' doesn't have two operands.",
InternalError::AST);
}
}
}

View File

@ -2420,9 +2420,10 @@ private:
ASSERT_EQUALS("", errout.str());
// #3714 - segmentation fault for syntax error
check("void f() {\n"
" if (()) { }\n"
"}");
ASSERT_THROW(check("void f() {\n"
" if (()) { }\n"
"}"),
InternalError);
// #3865
check("void f() {\n"

View File

@ -471,6 +471,7 @@ private:
TEST_CASE(checkTemplates);
TEST_CASE(checkNamespaces);
TEST_CASE(checkLambdas);
TEST_CASE(checkIfCppCast);
// #9052
TEST_CASE(noCrash1);
@ -946,6 +947,7 @@ private:
ASSERT_EQUALS("; __published: ;", tokenizeAndStringify(";__published:;", false));
ASSERT_EQUALS("a . public : ;", tokenizeAndStringify("a.public:;", false));
ASSERT_EQUALS("void f ( x & = 2 ) ;", tokenizeAndStringify("void f(x &= 2);", false));
ASSERT_EQUALS("const_cast < a * > ( & e )", tokenizeAndStringify("const_cast<a*>(&e)", false));
}
void concatenateNegativeNumber() {
@ -8078,6 +8080,20 @@ private:
" }\n"
"};\n"))
}
void checkIfCppCast() {
ASSERT_NO_THROW(tokenizeAndStringify("struct a {\n"
" int b();\n"
"};\n"
"struct c {\n"
" bool d() const;\n"
" a e;\n"
"};\n"
"bool c::d() const {\n"
" int f = 0;\n"
" if (!const_cast<a *>(&e)->b()) {}\n"
" return f;\n"
"}\n"))
}
void noCrash1() {
ASSERT_NO_THROW(tokenizeAndStringify(