new can initialize memory, don't warn in this case (#7623)

This commit is contained in:
Harald Scheidl 2016-10-07 21:30:23 +02:00 committed by PKEuS
parent b9f11f246d
commit 9f1b70fa04
2 changed files with 28 additions and 0 deletions

View File

@ -649,8 +649,26 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
}
if (var.isPointer() && (var.typeStartToken()->isStandardType() || var.typeStartToken()->isEnumType() || (var.type() && var.type()->needInitialization == Type::True)) && Token::simpleMatch(tok->next(), "= new")) {
*alloc = CTOR_CALL;
// type has constructor(s)
if (var.typeScope() && var.typeScope()->numConstructors > 0)
return true;
// 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% [")) {
const Token* tokClosingBracket=tok->tokAt(4)->link();
// array new with initialization
if (tokClosingBracket && Token::simpleMatch(tokClosingBracket->next(), "( )"))
return true;
}
}
continue;
}

View File

@ -1667,6 +1667,16 @@ private:
" font->Initialize();\n"
"}");
ASSERT_EQUALS("", errout.str());
// #7623 - new can also initialize the memory, don't warn in this case
checkUninitVar("void foo(){\n"
" int* p1 = new int(314); \n"
" int* p2 = new int(); \n"
" int* arr = new int[5](); \n"
" std::cout << *p1 << *p2 << arr[0]; \n"
"}");
ASSERT_EQUALS("", errout.str());
}
// class / struct..