From 091a81741943cb0e162cc65aa98850a8a796bcd4 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 17 Mar 2021 22:58:29 -0500 Subject: [PATCH] Report an error if analysis becomes cyclic --- lib/forwardanalyzer.cpp | 4 ++++ lib/reverseanalyzer.cpp | 5 +++++ lib/tokenize.cpp | 2 ++ 3 files changed, 11 insertions(+) diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index c258ee04d0d..85a0610ef9a 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -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.. diff --git a/lib/reverseanalyzer.cpp b/lib/reverseanalyzer.cpp index c55b2906733..f11333c1527 100644 --- a/lib/reverseanalyzer.cpp +++ b/lib/reverseanalyzer.cpp @@ -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; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a6fa112fd95..bb9a1adfcee 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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();