Skip to content

Commit

Permalink
Fix #12142 FP uninitStructMember, unreadVariable with pointer to memb…
Browse files Browse the repository at this point in the history
…er (danmar#5618)
  • Loading branch information
chrchr-github authored Nov 4, 2023
1 parent fa7891e commit 72a3617
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions lib/fwdanalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4875,6 +4875,15 @@ class TestUninitVar : public TestFixture {
" 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() {
Expand Down
12 changes: 12 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class TestUnusedVar : public TestFixture {
TEST_CASE(localvarStruct11); // 10095
TEST_CASE(localvarStruct12); // #10495
TEST_CASE(localvarStruct13); // #10398
TEST_CASE(localvarStruct14);
TEST_CASE(localvarStructArray);
TEST_CASE(localvarUnion1);

Expand Down Expand Up @@ -5330,6 +5331,17 @@ class TestUnusedVar : public TestFixture {
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;};

Expand Down

0 comments on commit 72a3617

Please sign in to comment.