Skip to content

Commit 54de731

Browse files
committed
Refactorized CheckUninitVar::checkScope(), fixed false negative
1 parent 5f36c7c commit 54de731

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,35 +1060,36 @@ void CheckUninitVar::checkScope(const Scope* scope)
10601060
if ((_tokenizer->isCPP() && i->type() && !i->isPointer() && i->type()->needInitialization != Type::True) ||
10611061
i->isStatic() || i->isExtern() || i->isConst() || i->isArray() || i->isReference())
10621062
continue;
1063+
10631064
// don't warn for try/catch exception variable
1064-
{
1065-
const Token *start = i->typeStartToken();
1066-
while (start && start->isName())
1067-
start = start->previous();
1068-
if (start && Token::simpleMatch(start->previous(), "catch ("))
1069-
continue;
1070-
}
1065+
if (i->isThrow())
1066+
continue;
1067+
10711068
if (i->nameToken()->strAt(1) == "(" || i->nameToken()->strAt(1) == "{")
10721069
continue;
1070+
1071+
if (Token::Match(i->nameToken(), "%var% =")) { // Variable is initialized, but Rhs might be not
1072+
checkRhs(i->nameToken(), *i, false, "");
1073+
continue;
1074+
}
1075+
10731076
bool stdtype = _tokenizer->isC();
10741077
const Token* tok = i->typeStartToken();
10751078
for (; tok && tok->str() != ";" && tok->str() != "<"; tok = tok->next()) {
10761079
if (tok->isStandardType())
10771080
stdtype = true;
10781081
}
1082+
10791083
while (tok && tok->str() != ";")
10801084
tok = tok->next();
10811085
if (!tok)
10821086
continue;
1083-
if (Token::Match(i->nameToken(), "%var% =")) {
1084-
checkRhs(i->nameToken(), *i, false, "");
1085-
continue;
1086-
}
1087+
10871088
if (stdtype || i->isPointer()) {
10881089
bool alloc = false;
10891090
checkScopeForVariable(scope, tok, *i, nullptr, nullptr, &alloc, "");
10901091
}
1091-
if (Token::Match(i->typeStartToken(), "struct %type% *| %var% ;"))
1092+
if (i->type())
10921093
checkStruct(scope, tok, *i);
10931094
}
10941095

test/testuninitvar.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,6 +2965,13 @@ class TestUninitVar : public TestFixture {
29652965
"}");
29662966
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
29672967

2968+
checkUninitVar2("struct AB { int a; int b; };\n"
2969+
"void f(void) {\n"
2970+
" AB ab;\n"
2971+
" int a = ab.a;\n"
2972+
"}");
2973+
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
2974+
29682975
checkUninitVar2("struct AB { int a; int b; };\n"
29692976
"void f(void) {\n"
29702977
" struct AB ab;\n"

0 commit comments

Comments
 (0)