Fix #10596 FN uninitdata with value initialization (#5056)

* Assign values to pointers with C++11 init

* Handle assigning empty init list

* Fix #10596 FN uninitdata with value initialization

* Fix test
This commit is contained in:
chrchr-github 2023-05-13 22:07:09 +02:00 committed by GitHub
parent 6a8c70c1b9
commit 096d3a78b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -565,7 +565,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
}
// = { .. }
else if (Token::simpleMatch(tok, "= {")) {
else if (Token::simpleMatch(tok, "= {") || (Token::Match(tok, "%name% {") && tok->variable() && tok == tok->variable()->nameToken())) {
// end token
const Token *end = tok->next()->link();
@ -657,7 +657,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
}
// Unknown or unhandled inner scope
else if (Token::simpleMatch(tok, ") {") || (Token::Match(tok, "%name% {") && tok->str() != "try")) {
else if (Token::simpleMatch(tok, ") {") || (Token::Match(tok, "%name% {") && tok->str() != "try" && !(tok->variable() && tok == tok->variable()->nameToken()))) {
if (tok->str() == "struct" || tok->str() == "union") {
tok = tok->linkAt(1);
continue;

View File

@ -2048,6 +2048,15 @@ private:
" A* a = new A{};\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("int f() {\n" // #10596
" int* a = new int;\n"
" int i{};\n"
" i += *a;\n"
" delete a;\n"
" return i;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: a\n", errout.str());
}
// class / struct..