From 72a36172aa569979f62842c9bddaa068474a5f6e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 4 Nov 2023 13:40:06 +0100 Subject: [PATCH] Fix #12142 FP uninitStructMember, unreadVariable with pointer to member (#5618) --- lib/checkuninitvar.cpp | 5 +++++ lib/fwdanalysis.cpp | 2 ++ test/testuninitvar.cpp | 9 +++++++++ test/testunusedvar.cpp | 12 ++++++++++++ 4 files changed, 28 insertions(+) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 98a39768c..e0c8b7db6 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -697,6 +697,11 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var return true; } + // bailout if there is a pointer to member + if (Token::Match(tok, "%varid% . *", var.declarationId())) { + return true; + } + if (tok->str() == "?") { if (!tok->astOperand2()) return true; diff --git a/lib/fwdanalysis.cpp b/lib/fwdanalysis.cpp index f2431fce3..1dd44759d 100644 --- a/lib/fwdanalysis.cpp +++ b/lib/fwdanalysis.cpp @@ -500,6 +500,8 @@ bool FwdAnalysis::possiblyAliased(const Token *expr, const Token *startToken) co { if (expr->isUnaryOp("*") && !expr->astOperand1()->isUnaryOp("&")) return true; + if (Token::simpleMatch(expr, ". *")) + return true; const bool macro = false; const bool pure = false; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 645caf390..0bd66b5cb 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -4875,6 +4875,15 @@ private: " return s;\n" "}"); ASSERT_EQUALS("", errout.str()); + + checkUninitVar("struct S { int i; };\n" // #12142 + "int f() {\n" + " S s;\n" + " int S::* p = &S::i;\n" + " s.*p = 123;\n" + " return s.i;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void uninitvar2_while() { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 8f6833625..57ce5632f 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -201,6 +201,7 @@ private: TEST_CASE(localvarStruct11); // 10095 TEST_CASE(localvarStruct12); // #10495 TEST_CASE(localvarStruct13); // #10398 + TEST_CASE(localvarStruct14); TEST_CASE(localvarStructArray); TEST_CASE(localvarUnion1); @@ -5330,6 +5331,17 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvarStruct14() { // #12142 + functionVariableUsage("struct S { int i; };\n" + "int f() {\n" + " S s;\n" + " int S::* p = &S::i;\n" + " s.*p = 123;\n" + " return s.i;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void localvarStructArray() { // extracttests.start: struct X {int a;};