From b0ac92ce8fe3cc97d1d21c5ae56149d194516795 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sat, 20 Mar 2021 08:02:07 -0500 Subject: [PATCH] Report an error if analysis becomes cyclic (#3173) --- 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 c258ee04d..85a0610ef 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 c55b29067..f11333c15 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 4abaa140e..d3a4c3cff 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();