From 3f89e38275ab341ad255b0201c7cf044f4987075 Mon Sep 17 00:00:00 2001 From: Ettl Martin Date: Wed, 19 Sep 2012 10:37:30 +0200 Subject: [PATCH] fixed #4195 segmentation fault of cppcheck (invalid code); Added a testcase --- lib/tokenize.cpp | 3 +++ test/testtokenize.cpp | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 221adf499..899e92ab1 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7282,6 +7282,9 @@ void Tokenizer::simplifyEnum() bool simplify = false; bool hasClass = false; EnumValue *ev = NULL; + + if (!tok1) + return; for (Token *tok2 = tok1->next(); tok2; tok2 = tok2->next()) { if (tok2->str() == "}") { --level; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 0dc018b63..85145f8c8 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -56,6 +56,7 @@ private: TEST_CASE(tokenize21); // tokenize 0x0E-7 TEST_CASE(tokenize22); // special marker $ from preprocessor TEST_CASE(tokenize23); // tokenize "return - __LINE__;" + TEST_CASE(tokenize24); // #4195 (segmentation fault) // don't freak out when the syntax is wrong TEST_CASE(wrong_syntax1); @@ -641,6 +642,11 @@ private: ASSERT_EQUALS("return -1 ;", tokenizeAndStringify("return - __LINE__;")); } + // #4195 - segfault for "enum { int f ( ) { return = } r = f ( ) ; }" + void tokenize24() { + tokenizeAndStringify("enum { int f ( ) { return = } r = f ( ) ; }"); + } + void wrong_syntax1() { { const std::string code("TR(kvmpio, PROTO(int rw), ARGS(rw), TP_(aa->rw;))");