From 479af214053406f577cc5d1f17c6dd66ab0fce02 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 24 Mar 2022 20:08:51 +0100 Subject: [PATCH] Fix #10698 FP uninitvar with new and braced initializer (#3934) --- lib/tokenlist.cpp | 4 +++- lib/valueflow.cpp | 2 +- test/testtokenize.cpp | 2 ++ test/testuninitvar.cpp | 6 ++++++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 1a9485261..933dd11d4 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -913,7 +913,9 @@ static bool isPrefixUnary(const Token* tok, bool cpp) static void compilePrecedence2(Token *&tok, AST_state& state) { - compileScope(tok, state); + const bool isStartOfCpp11Init = state.cpp && tok && tok->str() == "{" && iscpp11init(tok); + if (!(isStartOfCpp11Init && Token::Match(tok->tokAt(-2), "new %type% {"))) + compileScope(tok, state); while (tok) { if (tok->tokType() == Token::eIncDecOp && !isPrefixUnary(tok, state.cpp)) { compileUnaryOp(tok, state, compileScope); diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index e47a28293..34ac3a7c7 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -7889,7 +7889,7 @@ static void valueFlowDynamicBufferSize(TokenList* tokenlist, SymbolDatabase* sym else { typeTok = newTok->astOperand1(); if (typeTok && typeTok->str() == "{") - typeTok = typeTok->astOperand2(); + typeTok = typeTok->astOperand1(); } if (bracTok && bracTok->astOperand2() && bracTok->astOperand2()->hasKnownIntValue()) numElem = bracTok->astOperand2()->getKnownIntValue(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index e24301c61..6bf4434ec 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -6030,6 +6030,8 @@ private: ASSERT_EQUALS("aA1(new(bB2(new(,", testAst("a(new A(1)), b(new B(2))")); ASSERT_EQUALS("Fred10[new", testAst(";new Fred[10];")); ASSERT_EQUALS("adelete", testAst("void f() { delete a; }")); + ASSERT_EQUALS("Aa*A{new=", testAst("A* a = new A{};")); + ASSERT_EQUALS("Aa*A12,{new=", testAst("A* a = new A{ 1, 2 };")); // placement new ASSERT_EQUALS("X12,3,(new ab,c,", testAst("new (a,b,c) X(1,2,3);")); diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 870cf6232..0298ff652 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2024,6 +2024,12 @@ private: " a = *pBuf;\n" "}", "test.c"); ASSERT_EQUALS("", errout.str()); + + checkUninitVar("class A {};\n" // #10698 + "void f() {\n" + " A* a = new A{};\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } // class / struct..