diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 7dc76b3ca..d0a03f7c5 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1333,7 +1333,7 @@ void CheckUninitVar::valueFlowUninit() for (const Scope &scope : symbolDatabase->scopeList) { if (!scope.isExecutable()) continue; - for (const Token* tok = scope.bodyStart; tok != scope.bodyEnd; tok = tok->next()) { + for (const Token* tok = scope.bodyStart; tok && tok != scope.bodyEnd; tok = tok->next()) { if (Token::simpleMatch(tok, "sizeof (")) { tok = tok->linkAt(1); continue; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 8914e55a8..c4982854d 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -83,6 +83,7 @@ private: TEST_CASE(valueFlowUninit); TEST_CASE(uninitvar_ipa); TEST_CASE(uninitvar_memberfunction); + TEST_CASE(uninitvar_nonmember); // crash in ycmd test TEST_CASE(isVariableUsageDeref); // *p @@ -4441,6 +4442,18 @@ private: ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: c\n", errout.str()); } + void uninitvar_nonmember() { + valueFlowUninit( "struct Foo {\n" + " int bar;\n" + "};\n" + "\n" + "int main() {\n" + " Foo* foo;\n" + " foo.b\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: foo\n", errout.str()); + } + void isVariableUsageDeref() { // *p checkUninitVar("void f() {\n"