Skip to content

Commit 096d3a7

Browse files
Fix #10596 FN uninitdata with value initialization (danmar#5056)
* Assign values to pointers with C++11 init * Handle assigning empty init list * Fix #10596 FN uninitdata with value initialization * Fix test
1 parent 6a8c70c commit 096d3a7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/checkuninitvar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
565565
}
566566

567567
// = { .. }
568-
else if (Token::simpleMatch(tok, "= {")) {
568+
else if (Token::simpleMatch(tok, "= {") || (Token::Match(tok, "%name% {") && tok->variable() && tok == tok->variable()->nameToken())) {
569569
// end token
570570
const Token *end = tok->next()->link();
571571

@@ -657,7 +657,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
657657
}
658658

659659
// Unknown or unhandled inner scope
660-
else if (Token::simpleMatch(tok, ") {") || (Token::Match(tok, "%name% {") && tok->str() != "try")) {
660+
else if (Token::simpleMatch(tok, ") {") || (Token::Match(tok, "%name% {") && tok->str() != "try" && !(tok->variable() && tok == tok->variable()->nameToken()))) {
661661
if (tok->str() == "struct" || tok->str() == "union") {
662662
tok = tok->linkAt(1);
663663
continue;

test/testuninitvar.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,15 @@ class TestUninitVar : public TestFixture {
20482048
" A* a = new A{};\n"
20492049
"}\n");
20502050
ASSERT_EQUALS("", errout.str());
2051+
2052+
checkUninitVar("int f() {\n" // #10596
2053+
" int* a = new int;\n"
2054+
" int i{};\n"
2055+
" i += *a;\n"
2056+
" delete a;\n"
2057+
" return i;\n"
2058+
"}\n");
2059+
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: a\n", errout.str());
20512060
}
20522061

20532062
// class / struct..

0 commit comments

Comments
 (0)