From 71b66209b7b14bc4ed13f5f102d505052253a9aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 20 Feb 2013 06:58:27 +0100 Subject: [PATCH] Fixed #4300 (segmentation fault of cppcheck (invalid code)) --- lib/tokenize.cpp | 2 +- test/testtokenize.cpp | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 311c50c9a..9f5d85343 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3752,7 +3752,7 @@ void Tokenizer::simplifyEmptyNamespaces() return; bool goback = false; - for (Token *tok = list.front(); tok; tok = tok->next()) { + for (Token *tok = list.front(); tok; tok = tok ? tok->next() : NULL) { if (goback) { tok = tok->previous(); goback = false; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 36df7f1f5..d682a004a 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -68,7 +68,8 @@ private: TEST_CASE(wrong_syntax4); // #3618 TEST_CASE(wrong_syntax_if_macro); // #2518 - if MACRO() TEST_CASE(syntax_case_default); - TEST_CASE(garbageCode); + TEST_CASE(garbageCode1); + TEST_CASE(garbageCode2); // #4300 TEST_CASE(foreach); // #3690 @@ -835,10 +836,14 @@ private: } } - void garbageCode() { + void garbageCode1() { tokenizeAndStringify("struct x foo_t; foo_t typedef y;"); } + void garbageCode2() { //#4300 (segmentation fault) + tokenizeAndStringify("enum { D = 1 struct { } ; } s.b = D;"); + } + void foreach() { // #3690 const std::string code("void f() { for each ( char c in MyString ) { Console::Write(c); } }");