From 7c8c0ef3a25e4e10d9864fbdb0f6f32580fa4fad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 9 Sep 2020 18:26:04 +0200 Subject: [PATCH] Bug hunting: Avoid uninit struct member false positives --- lib/exprengine.cpp | 2 +- test/testbughuntingchecks.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/exprengine.cpp b/lib/exprengine.cpp index 9050c2c1a..a624a124d 100644 --- a/lib/exprengine.cpp +++ b/lib/exprengine.cpp @@ -2512,7 +2512,7 @@ static ExprEngine::ValuePtr createStructVal(const Scope *structScope, bool unini std::shared_ptr structValue = std::make_shared(data.getNewSymbolName()); auto uninitValue = std::make_shared(); for (const Variable &member : structScope->varlist) { - if (uninitData) { + if (uninitData && !member.isInit()) { if (member.isPointer()) { structValue->member[member.name()] = uninitValue; continue; diff --git a/test/testbughuntingchecks.cpp b/test/testbughuntingchecks.cpp index f4aba5233..5229224b4 100644 --- a/test/testbughuntingchecks.cpp +++ b/test/testbughuntingchecks.cpp @@ -44,6 +44,7 @@ private: TEST_CASE(uninit_bailout); TEST_CASE(uninit_fp_smartptr); TEST_CASE(uninit_fp_struct); + TEST_CASE(uninit_fp_struct_member_init_2); TEST_CASE(uninit_fp_template_var); TEST_CASE(ctu); #endif @@ -202,6 +203,16 @@ private: ASSERT_EQUALS("", errout.str()); } + void uninit_fp_struct_member_init_2() { + check("struct A {\n" + " int x {0}; int y {0};\n" + "};\n" + "void foo(const A& a) {\n" + " bar(a);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void uninit_fp_template_var() { check("void foo() {\n" " X*x = DYNAMIC_CAST(X, p);\n"