From c6e2107d99becc0518e1d0ba239e50a97a15150d Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Wed, 22 Oct 2014 20:25:36 +0200 Subject: [PATCH] Ticket #6187: Avoid infinite loop if eraseDeadCode does not remove anything (this time without memory corruption). --- lib/tokenize.cpp | 9 ++++++--- test/testsimplifytokens.cpp | 10 ++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 7be8059a4..1b7652999 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8967,10 +8967,13 @@ void Tokenizer::simplifyWhile0() // remove "while (0) { .. }" if (Token::simpleMatch(tok->next()->link(), ") {")) { - Token *end = tok->next()->link(); + Token *end = tok->next()->link(), *old_prev = tok->previous(); end = end->next()->link(); - tok = tok->previous(); - eraseDeadCode(tok, end->next()); + eraseDeadCode(old_prev, end->next()); + if (old_prev && old_prev->next()) + tok = old_prev->next(); + else + break; } } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 288318a2c..352d21ecb 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -2393,6 +2393,16 @@ private: ASSERT_EQUALS("void foo ( ) { exit ( 0 ) ; }", tokWithStdLib("void foo() { do { exit(0); } while (true); }")); + + // #6187 + tokWithStdLib("void foo() {\n" + " goto label;\n" + " for (int i = 0; i < 0; ++i) {\n" + " ;\n" + "label:\n" + " ;\n" + " }\n" + "}"); } void strcat1() {