Fix #10200 False positive: uninitStructMember (#3741)

This commit is contained in:
chrchr-github 2022-01-25 12:14:20 +01:00 committed by GitHub
parent ea81ce933e
commit f49cfcd23c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 0 deletions

View File

@ -196,6 +196,8 @@ void CheckUninitVar::checkScope(const Scope* scope, const std::set<std::string>
void CheckUninitVar::checkStruct(const Token *tok, const Variable &structvar) void CheckUninitVar::checkStruct(const Token *tok, const Variable &structvar)
{ {
const Token *typeToken = structvar.typeStartToken(); const Token *typeToken = structvar.typeStartToken();
while (Token::Match(typeToken, "%name% ::"))
typeToken = typeToken->tokAt(2);
const SymbolDatabase * symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase * symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope *scope2 : symbolDatabase->classAndStructScopes) { for (const Scope *scope2 : symbolDatabase->classAndStructScopes) {
if (scope2->className == typeToken->str() && scope2->numConstructors == 0U) { if (scope2->className == typeToken->str() && scope2->numConstructors == 0U) {

View File

@ -6052,6 +6052,55 @@ private:
" if ((&a)->bar) ;\n" " if ((&a)->bar) ;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
valueFlowUninit("struct A {\n" // #10200
" struct B {\n"
" int i;\n"
" };\n"
" int j;\n"
"};\n"
"void f(std::vector<A::B>& x) {\n"
" A::B b;\n"
" b.i = 123;\n"
" x.push_back(b);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
valueFlowUninit("struct A {\n"
" struct B {\n"
" int i;\n"
" };\n"
" int j;\n"
"};\n"
"void f(std::vector<A::B>& x) {\n"
" A::B b;\n"
" x.push_back(b);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9]: (error) Uninitialized variable: b\n", errout.str());
valueFlowUninit("struct A {\n"
" struct B {\n"
" int i;\n"
" };\n"
" int j;\n"
"};\n"
"void f(std::vector<A>&x) {\n"
" A a;\n"
" a.j = 123;\n"
" x.push_back(a);\n"
"}\n");
valueFlowUninit("struct A {\n"
" struct B {\n"
" int i;\n"
" };\n"
" int j;\n"
"};\n"
"void f(std::vector<A>& x) {\n"
" A a;\n"
" x.push_back(a);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9]: (error) Uninitialized variable: a\n", errout.str());
} }
void ctu_(const char* file, int line, const char code[]) { void ctu_(const char* file, int line, const char code[]) {