Report an error if analysis becomes cyclic (#3173)

This commit is contained in:
Paul Fultz II 2021-03-20 08:02:07 -05:00 committed by GitHub
parent 390a5af064
commit b0ac92ce8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 0 deletions

View File

@ -385,8 +385,12 @@ struct ForwardTraversal {
Progress updateRange(Token* start, const Token* end, int depth = 20) {
if (depth < 0)
return Break(Terminate::Bail);
std::size_t i = 0;
for (Token* tok = start; tok && tok != end; tok = tok->next()) {
Token* next = nullptr;
if (tok->index() <= i)
throw InternalError(tok, "Cyclic forward analysis.");
i = tok->index();
if (tok->link()) {
// Skip casts..

View File

@ -1,6 +1,7 @@
#include "reverseanalyzer.h"
#include "analyzer.h"
#include "astutils.h"
#include "errortypes.h"
#include "forwardanalyzer.h"
#include "settings.h"
#include "symboldatabase.h"
@ -119,7 +120,11 @@ struct ReverseTraversal {
void traverse(Token* start, const Token* end = nullptr) {
if (start == end)
return;
std::size_t i = start->index();
for (Token* tok = start->previous(); tok != end; tok = tok->previous()) {
if (tok->index() >= i)
throw InternalError(tok, "Cyclic reverse analysis.");
i = tok->index();
if (tok == start || (tok->str() == "{" && (tok->scope()->type == Scope::ScopeType::eFunction ||
tok->scope()->type == Scope::ScopeType::eLambda))) {
break;

View File

@ -5281,6 +5281,8 @@ bool Tokenizer::simplifyTokenList2()
Token::assignProgressValues(list.front());
list.front()->assignIndexes();
list.createAst();
// needed for #7208 (garbage code) and #7724 (ast max depth limit)
list.validateAst();