Fix #11018 FP uninitvar with redundant pointer op (#4082)

* Skip redundant pointer op

* Fix  #11018 FP uninitvar with redundant pointer op

* Format
This commit is contained in:
chrchr-github 2022-05-05 06:54:27 +02:00 committed by GitHub
parent aebc080c0f
commit adba751217
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View File

@ -2241,6 +2241,18 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings,
if (Token::Match(tok2->astParent(), "++|--")) if (Token::Match(tok2->astParent(), "++|--"))
return true; return true;
auto skipRedundantPtrOp = [](const Token* tok, const Token* parent) {
const Token* gparent = parent ? parent->astParent() : nullptr;
while (parent && gparent && ((parent->isUnaryOp("*") && gparent->isUnaryOp("&")) || ((parent->isUnaryOp("&") && gparent->isUnaryOp("*"))))) {
tok = gparent;
parent = gparent->astParent();
if (parent)
gparent = parent->astParent();
}
return tok;
};
tok2 = skipRedundantPtrOp(tok2, tok2->astParent());
if (tok2->astParent() && tok2->astParent()->isAssignmentOp()) { if (tok2->astParent() && tok2->astParent()->isAssignmentOp()) {
if (tok2 == tok2->astParent()->astOperand1()) if (tok2 == tok2->astParent()->astOperand1())
return true; return true;

View File

@ -6373,6 +6373,17 @@ private:
" x.push_back(a);\n" " x.push_back(a);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:9]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:9]: (error) Uninitialized variable: a\n", errout.str());
valueFlowUninit("struct S { struct T { int* p; } t[2]; };\n" // #11018
"void f() {\n"
" S s;\n"
" *&s.t[0].p = 0;\n"
"}\n"
"void g() {\n"
" S s;\n"
" ((*&(*&s.t[0].p))) = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void ctu_(const char* file, int line, const char code[]) { void ctu_(const char* file, int line, const char code[]) {