Skip to content

Commit ee14ea4

Browse files
committed
Fixed cppcheck-opensource#4907 (False positive "uninitStructMember" on structs with unions)
1 parent 6445406 commit ee14ea4

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,11 +1101,22 @@ void CheckUninitVar::checkScope(const Scope* scope)
11011101
for (std::size_t j = 0U; j < symbolDatabase->classAndStructScopes.size(); ++j) {
11021102
const Scope *scope2 = symbolDatabase->classAndStructScopes[j];
11031103
if (scope2->className == structname && scope2->numConstructors == 0U) {
1104+
bool hasUnion = false;
1105+
for (std::list<Scope*>::const_iterator childscope = scope2->nestedList.begin();
1106+
childscope != scope2->nestedList.end();
1107+
++childscope) {
1108+
if ((*childscope)->type == Scope::eUnion)
1109+
hasUnion = true;
1110+
}
1111+
if (hasUnion)
1112+
break;
1113+
11041114
for (std::list<Variable>::const_iterator it = scope2->varlist.begin(); it != scope2->varlist.end(); ++it) {
11051115
const Variable &var = *it;
11061116
if (!var.isArray())
11071117
checkScopeForVariable(scope, tok, *i, NULL, NULL, var.name());
11081118
}
1119+
break;
11091120
}
11101121
}
11111122
}

test/testuninitvar.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,6 +2788,20 @@ class TestUninitVar : public TestFixture {
27882788
"}\n", "test.c");
27892789
ASSERT_EQUALS("", errout.str());
27902790

2791+
checkUninitVar2("struct PIXEL {\n" // union in struct #4970
2792+
" union {\n"
2793+
" struct { unsigned char red,green,blue,alpha; };\n"
2794+
" unsigned int color;\n"
2795+
" };\n"
2796+
"};\n"
2797+
"\n"
2798+
"unsigned char f() {\n"
2799+
" struct PIXEL p1;\n"
2800+
" p1.color = 255;\n"
2801+
" return p1.red;\n"
2802+
"}");
2803+
ASSERT_EQUALS("", errout.str());
2804+
27912805
// return
27922806
checkUninitVar2("struct AB { int a; int b; };\n"
27932807
"void f(void) {\n"

0 commit comments

Comments
 (0)