From 4168d79b099561fa0cdc59e61b9ad8793edaf085 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Mon, 15 Aug 2011 07:07:12 -0400 Subject: [PATCH] fix #3005 (segmentation fault of cppcheck ( enum : x )) --- lib/tokenize.cpp | 12 +++++++++--- test/testsimplifytokens.cpp | 10 +++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 7f1d44a4c..24896f843 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8294,19 +8294,25 @@ void Tokenizer::simplifyEnum() if (!tok->next()) { syntaxError(tok); - return; + return; // can't recover } typeTokenStart = tok->next(); tok = tok->next(); typeTokenEnd = typeTokenStart; - while (typeTokenEnd->next()->str() == "::" || - Token::Match(typeTokenEnd->next(), "%type%")) + while (typeTokenEnd->next() && (typeTokenEnd->next()->str() == "::" || + Token::Match(typeTokenEnd->next(), "%type%"))) { typeTokenEnd = typeTokenEnd->next(); tok = tok->next(); } + + if (!tok->next()) + { + syntaxError(tok); + return; // can't recover + } } // check for forward declaration diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 6517e4ceb..d4e8b42d6 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -321,6 +321,7 @@ private: TEST_CASE(enum24); // ticket #2828 TEST_CASE(enum25); // ticket #2966 TEST_CASE(enum26); // ticket #2975 (segmentation fault) + TEST_CASE(enum27); // ticket #3005 (segmentation fault) // remove "std::" on some standard functions TEST_CASE(removestd); @@ -6737,13 +6738,20 @@ private: ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); } - void enum26() // ticket #2978 (segmentation fault) + void enum26() // ticket #2975 (segmentation fault) { const char code[] = "enum E {} e enum\n"; tok(code, false); ASSERT_EQUALS("", errout.str()); } + void enum27() // ticket #3005 (segmentation fault) + { + const char code[] = "enum : x\n"; + tok(code, false); + ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); + } + void removestd() { ASSERT_EQUALS("; strcpy ( a , b ) ;", tok("; std::strcpy(a,b);"));