Fix #12142 FP uninitStructMember, unreadVariable with pointer to member (#5618)

This commit is contained in:
chrchr-github 2023-11-04 13:40:06 +01:00 committed by GitHub
parent fa7891e37b
commit 72a36172aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 0 deletions

View File

@ -697,6 +697,11 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
return true; return true;
} }
// bailout if there is a pointer to member
if (Token::Match(tok, "%varid% . *", var.declarationId())) {
return true;
}
if (tok->str() == "?") { if (tok->str() == "?") {
if (!tok->astOperand2()) if (!tok->astOperand2())
return true; return true;

View File

@ -500,6 +500,8 @@ bool FwdAnalysis::possiblyAliased(const Token *expr, const Token *startToken) co
{ {
if (expr->isUnaryOp("*") && !expr->astOperand1()->isUnaryOp("&")) if (expr->isUnaryOp("*") && !expr->astOperand1()->isUnaryOp("&"))
return true; return true;
if (Token::simpleMatch(expr, ". *"))
return true;
const bool macro = false; const bool macro = false;
const bool pure = false; const bool pure = false;

View File

@ -4875,6 +4875,15 @@ private:
" return s;\n" " return s;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); 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() { void uninitvar2_while() {

View File

@ -201,6 +201,7 @@ private:
TEST_CASE(localvarStruct11); // 10095 TEST_CASE(localvarStruct11); // 10095
TEST_CASE(localvarStruct12); // #10495 TEST_CASE(localvarStruct12); // #10495
TEST_CASE(localvarStruct13); // #10398 TEST_CASE(localvarStruct13); // #10398
TEST_CASE(localvarStruct14);
TEST_CASE(localvarStructArray); TEST_CASE(localvarStructArray);
TEST_CASE(localvarUnion1); TEST_CASE(localvarUnion1);
@ -5330,6 +5331,17 @@ private:
ASSERT_EQUALS("", errout.str()); 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() { void localvarStructArray() {
// extracttests.start: struct X {int a;}; // extracttests.start: struct X {int a;};