Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -784,12 +784,16 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
// standard or enum type: check if new initializes the allocated memory
if (var.typeStartToken()->isStandardType() || var.typeStartToken()->isEnumType()) {
// scalar new with initialization
if (Token::Match(tok->next(), "= new %type% ("))
return true;

// array new
if (Token::Match(tok->next(), "= new %type% [") && Token::simpleMatch(tok->linkAt(4), "] ("))
return true;
if (Token::Match(tok->next(), "= new ::|%type%")) {
const Token* initTok = tok->tokAt(4);
while (Token::Match(initTok, "::|%type%"))
initTok = initTok->next();
if (Token::simpleMatch(initTok, "["))
initTok = initTok->link()->next();
if (Token::Match(initTok, "[({]"))
return true;
}
}

continue;
Expand Down
20 changes: 20 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2181,6 +2181,26 @@ class TestUninitVar : public TestFixture {
" return s;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

checkUninitVar("int f() {\n" // #14719
" int* p = new int[]{ 1, 2 };\n"
" int i = p[0] + p[1];\n"
" delete[] p;\n"
" return i;\n"
"}\n"
"int g() {\n"
" int* p = new std::int32_t[]{ 1, 2 };\n"
" int i = p[0] + p[1];\n"
" delete[] p;\n"
" return i;\n"
"}\n"
"int h() {\n"
" int* p = new ::std::int32_t[]{ 1, 2 };\n"
" int i = p[0] + p[1];\n"
" delete[] p;\n"
" return i;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

// class / struct..
Expand Down
Loading