new can initialize memory, don't warn in this case (#7623)
This commit is contained in:
parent
b9f11f246d
commit
9f1b70fa04
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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..
|
||||
|
|
Loading…
Reference in New Issue