From 3b9c399f72e7d2c94bf06a8cafc5b27affd646d2 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Wed, 7 Jul 2021 01:20:32 -0500 Subject: [PATCH] Fix 10338: Hang/Crash in valueflow (#3328) --- lib/astutils.cpp | 2 +- lib/forwardanalyzer.cpp | 2 +- test/testvalueflow.cpp | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index a220cfb99..891a836ee 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -597,7 +597,7 @@ bool precedes(const Token * tok1, const Token * tok2) if (!tok1) return false; if (!tok2) - return false; + return true; return tok1->index() < tok2->index(); } diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 75035728b..91122f504 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -471,7 +471,7 @@ struct ForwardTraversal { if (depth < 0) return Break(Analyzer::Terminate::Bail); std::size_t i = 0; - for (Token* tok = start; tok && tok != end; tok = tok->next()) { + for (Token* tok = start; precedes(tok, end); tok = tok->next()) { Token* next = nullptr; if (tok->index() <= i) throw InternalError(tok, "Cyclic forward analysis."); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 52b351282..295b3a73e 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -5630,6 +5630,15 @@ private: " return e;\n" "}\n"; valueOfTok(code, "x"); + + code = "void a() {\n" + " int b = 0;\n" + " do {\n" + " for (;;)\n" + " break;\n" + " } while (b < 1);\n" + "}\n"; + valueOfTok(code, "b"); } void valueFlowCrashConstructorInitialization() { // #9577