ValueFlow: Fix uninitvar false positive after initialization '*((int*)&x) = ..' (#5142)

This commit is contained in:
Daniel Marjamäki 2023-06-10 15:22:17 +02:00 committed by GitHub
parent 3c8caac772
commit 1c28457d2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -7924,6 +7924,14 @@ static Token* findStartToken(const Variable* var, Token* start, const Library* l
Token* first = uses.front();
if (Token::findmatch(start, "goto|asm|setjmp|longjmp", first))
return start;
if (first != var->nameToken()) {
// if this is lhs in assignment then set first to the first token in LHS expression
Token* temp = first;
while (Token::Match(temp->astParent(), "[&*(]") && precedes(temp->astParent(), temp))
temp = temp->astParent();
if (Token::simpleMatch(temp->astParent(), "=") && precedes(temp, temp->astParent()))
first = temp;
}
// If there is only one usage
if (uses.size() == 1)
return first->previous();

View File

@ -5292,6 +5292,18 @@ private:
"}";
ASSERT_EQUALS(0U, tokenValues(code, "x )").size());
// initialization
code = "int foo() {\n"
" int x;\n"
" *((int *)(&x)) = 12;"
" a = x + 1;\n"
"}";
values = tokenValues(code, "x +");
ASSERT_EQUALS(true, values.empty());
// ASSERT_EQUALS(1U, values.size());
// ASSERT(values.front().isIntValue());
// ASSERT_EQUALS(12, values.front().intvalue);
// #8036
code = "void foo() {\n"
" int x;\n"