From 096d3a78b050c46166ed63b65af36c9a1f0885e2 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 13 May 2023 22:07:09 +0200 Subject: [PATCH] Fix #10596 FN uninitdata with value initialization (#5056) * Assign values to pointers with C++11 init * Handle assigning empty init list * Fix #10596 FN uninitdata with value initialization * Fix test --- lib/checkuninitvar.cpp | 4 ++-- test/testuninitvar.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 622beae8d..9ef52b334 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -565,7 +565,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var } // = { .. } - else if (Token::simpleMatch(tok, "= {")) { + else if (Token::simpleMatch(tok, "= {") || (Token::Match(tok, "%name% {") && tok->variable() && tok == tok->variable()->nameToken())) { // end token const Token *end = tok->next()->link(); @@ -657,7 +657,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var } // Unknown or unhandled inner scope - else if (Token::simpleMatch(tok, ") {") || (Token::Match(tok, "%name% {") && tok->str() != "try")) { + else if (Token::simpleMatch(tok, ") {") || (Token::Match(tok, "%name% {") && tok->str() != "try" && !(tok->variable() && tok == tok->variable()->nameToken()))) { if (tok->str() == "struct" || tok->str() == "union") { tok = tok->linkAt(1); continue; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 6a9a9d9dd..032d86f98 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2048,6 +2048,15 @@ private: " A* a = new A{};\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + checkUninitVar("int f() {\n" // #10596 + " int* a = new int;\n" + " int i{};\n" + " i += *a;\n" + " delete a;\n" + " return i;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: a\n", errout.str()); } // class / struct..