diff --git a/lib/checkother.cpp b/lib/checkother.cpp index ff5c7e007..5b6bd17f8 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1336,7 +1336,7 @@ void CheckOther::checkVariableScope() } bool reduce = true; bool used = false; // Don't warn about unused variables - for (; tok != var->scope()->classEnd; tok = tok->next()) { + for (; tok && tok != var->scope()->classEnd; tok = tok->next()) { if (tok->str() == "{" && tok->scope() != tok->previous()->scope() && !tok->isExpandedMacro() && tok->scope()->type != Scope::eLambda) { if (used) { bool used2 = false; diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 9e5da993d..beb9d5a44 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -51,6 +51,7 @@ private: TEST_CASE(garbageCode9); // #5604 TEST_CASE(garbageCode10); // #6127 TEST_CASE(garbageCode11); + TEST_CASE(garbageCode12); TEST_CASE(astGarbage); } @@ -60,6 +61,12 @@ private: Settings settings; settings.debugwarnings = true; + settings.addEnabled("style"); + settings.addEnabled("warning"); + settings.addEnabled("portability"); + settings.addEnabled("performance"); + settings.inconclusive = true; + settings.experimental = true; // tokenize.. Tokenizer tokenizer(&settings, this); @@ -238,6 +245,10 @@ private: checkCode("( ) &"); } + void garbageCode12() { // do not crash + checkCode("{ g; S (void) { struct } { } int &g; }"); + } + void astGarbage() { checkCode("--"); // don't crash