Ticket #8352: Properly detect AST cycles. (#1060)

This commit is contained in:
Simon Martin 2018-02-10 14:39:57 +01:00 committed by Daniel Marjamäki
parent c26ae955ad
commit 66d16b51f0
2 changed files with 10 additions and 4 deletions

View File

@ -1078,13 +1078,13 @@ std::string Token::stringifyList(bool varid) const
void Token::astOperand1(Token *tok)
{
const Token* const root = tok;
if (_astOperand1)
_astOperand1->_astParent = nullptr;
// goto parent operator
if (tok) {
std::set<Token*> visitedParents;
while (tok->_astParent) {
if (tok->_astParent == this || tok->_astParent == root) // #6838/#6726 avoid hang on garbage code
if (!visitedParents.insert(tok->_astParent).second) // #6838/#6726/#8352 avoid hang on garbage code
throw InternalError(this, "Internal error. Token::astOperand1() cyclic dependency.");
tok = tok->_astParent;
}
@ -1095,14 +1095,14 @@ void Token::astOperand1(Token *tok)
void Token::astOperand2(Token *tok)
{
const Token* const root = tok;
if (_astOperand2)
_astOperand2->_astParent = nullptr;
// goto parent operator
if (tok) {
std::set<Token*> visitedParents;
while (tok->_astParent) {
//std::cout << tok << " -> " << tok->_astParent ;
if (tok->_astParent == this || tok->_astParent == root) // #6838/#6726 avoid hang on garbage code
if (!visitedParents.insert(tok->_astParent).second) // #6838/#6726 avoid hang on garbage code
throw InternalError(this, "Internal error. Token::astOperand2() cyclic dependency.");
tok = tok->_astParent;
}

View File

@ -1253,6 +1253,12 @@ private:
"b[i + 3] = a[i] * {}"), InternalError); // Don't hang (#5787)
checkCode("START_SECTION([EXTRA](bool isValid(const String &filename)))"); // Don't crash (#5991)
// #8352
ASSERT_THROW(checkCode("else return % name5 name2 - =name1 return enum | { - name3 1 enum != >= 1 >= ++ { { || "
"{ return return { | { - name3 1 enum != >= 1 >= ++ { name6 | ; ++}}}}}}}"), InternalError);
ASSERT_THROW(checkCode("else return % name5 name2 - =name1 return enum | { - name3 1 enum != >= 1 >= ++ { { || "
"{ return return { | { - name3 1 enum != >= 1 >= ++ { { || ; ++}}}}}}}}"), InternalError);
}
void templateSimplifierCrashes() {