diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 520fe24ea..d36cd5f40 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2859,6 +2859,8 @@ Check::FileInfo *CheckClass::getFileInfo(const Tokenizer *tokenizer, const Setti name = scope->className + "::" + name; scope = scope->nestedIn; } + if (name.empty()) + continue; name.erase(name.size() - 2); if (scope->type != Scope::ScopeType::eGlobal) continue; @@ -2874,7 +2876,7 @@ Check::FileInfo *CheckClass::getFileInfo(const Tokenizer *tokenizer, const Setti for (const Token *tok = classScope->classDef; tok != classScope->bodyEnd; tok = tok->next()) def += tok->str(); for (const Function &f: classScope->functionList) { - if (f.functionScope->nestedIn != classScope) { + if (f.functionScope && f.functionScope->nestedIn != classScope) { for (const Token *tok = f.functionScope->bodyStart; tok != f.functionScope->bodyEnd; tok = tok->next()) def += tok->str(); } diff --git a/test/testclass.cpp b/test/testclass.cpp index f46fb1762..0636dee96 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -225,6 +225,8 @@ private: TEST_CASE(unsafeClassRefMember); TEST_CASE(ctuOneDefinitionRule); + + TEST_CASE(getFileInfo); } void checkCopyCtorAndEqOperator(const char code[]) { @@ -7429,6 +7431,28 @@ private: ASSERT_EQUALS("", errout.str()); } + void getFileInfo(const char code[]) { + // Clear the error log + errout.str(""); + + // Tokenize.. + Tokenizer tokenizer(&settings1, this); + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + // Check.. + CheckClass checkClass(&tokenizer, &settings1, this); + + Check::FileInfo * fileInfo = checkClass.getFileInfo(&tokenizer, &settings1); + + delete fileInfo; + } + + void getFileInfo() { + getFileInfo("void foo() { union { struct { }; }; }"); // don't crash + getFileInfo("struct sometype { sometype(); }; sometype::sometype() = delete;"); // don't crash + } + }; REGISTER_TEST(TestClass)