fix crash in CheckClass::getFileInfo (#3172)

* fix crash in CheckClass::getFileInfo

* fix another crash

* fix memory leak

Co-authored-by: Robert Reif <reif@FX6840>
This commit is contained in:
IOBYTE 2021-03-19 04:19:48 -04:00 committed by GitHub
parent b1b7fbb63a
commit 1874b9cb0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -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();
}

View File

@ -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)