From 2d8993319cc39a46a6fc3f63f4acebe37fdad5e0 Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Wed, 15 Jul 2015 21:35:46 +0200 Subject: [PATCH] Ticket #6810: Avoid infinite loop upon invalid typed enum declaration. --- lib/tokenize.cpp | 7 ++++++- test/testsimplifytokens.cpp | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 576da850c..168a28149 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7597,11 +7597,16 @@ void Tokenizer::simplifyEnum() typeTokenEnd = 0; while (tok->next() && Token::Match(tok->next(), "::|%type%")) { + // Ticket #6810: Avoid infinite loop upon invalid enum definition + if (enumType && enumType->str() == tok->strAt(1)) { + typeTokenEnd = 0; + break; + } typeTokenEnd = tok->next(); tok = tok->next(); } - if (!tok->next() || !typeTokenEnd) { + if (!tok->next() || tok->str() == "::" || !typeTokenEnd) { syntaxError(tok); return; // can't recover } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index dd523a604..375dc7408 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -3188,6 +3188,13 @@ private: ASSERT_EQUALS(expected, checkSimplifyEnum(code, false)); // Compile as C code: enum has name 'class' checkSimplifyEnum(code, true); // Compile as C++ code: Don't crash } + + { + // Ticket #6810 + ASSERT_THROW(checkSimplifyEnum("enum x : enum x {} :"), InternalError); + ASSERT_THROW(checkSimplifyEnum("enum x : enum x {} () :"), InternalError); + ASSERT_THROW(checkSimplifyEnum("enum x : :: {} () :"), InternalError); + } } void enum16() { // ticket #1988