diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 0c9830b81..49fd55f34 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -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; } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 265227385..54fbb674c 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -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..