Fix #11777 (False positive: uninitialized variable, handling 'false ||' in valueflow) (#5169)

This commit is contained in:
Daniel Marjamäki 2023-06-17 21:08:22 +02:00 committed by GitHub
parent 082331c210
commit 38b2c5ee3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View File

@ -7852,14 +7852,14 @@ bool findTokenSkipDeadCodeImpl(const Library* library, Token* start, const Token
tok = thenStart->link(); tok = thenStart->link();
} }
} else if (Token::Match(tok->astParent(), "&&|?|%oror%") && astIsLHS(tok) && tok->hasKnownIntValue()) { } else if (Token::Match(tok->astParent(), "&&|?|%oror%") && astIsLHS(tok) && tok->hasKnownIntValue()) {
int r = tok->values().front().intvalue; const bool cond = tok->values().front().intvalue != 0;
Token* next = nullptr; Token* next = nullptr;
if ((r == 0 && Token::simpleMatch(tok->astParent(), "||")) || if ((cond && Token::simpleMatch(tok->astParent(), "||")) ||
(r != 0 && Token::simpleMatch(tok->astParent(), "&&"))) { (!cond && Token::simpleMatch(tok->astParent(), "&&"))) {
next = nextAfterAstRightmostLeaf(tok->astParent()); next = nextAfterAstRightmostLeaf(tok->astParent());
} else if (Token::simpleMatch(tok->astParent(), "?")) { } else if (Token::simpleMatch(tok->astParent(), "?")) {
Token* colon = tok->astParent()->astOperand2(); Token* colon = tok->astParent()->astOperand2();
if (r == 0) { if (!cond) {
next = colon; next = colon;
} else { } else {
if (findTokenSkipDeadCodeImpl(library, tok->astParent()->next(), colon, pred, found)) if (findTokenSkipDeadCodeImpl(library, tok->astParent()->next(), colon, pred, found))

View File

@ -5554,6 +5554,17 @@ private:
"}\n"; "}\n";
values = tokenValues(code, ". id", ValueFlow::Value::ValueType::UNINIT); values = tokenValues(code, ". id", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size()); ASSERT_EQUALS(0, values.size());
// #11777 - false || ...
code = "bool init(int *p);\n"
"\n"
"void uninitvar_FP9() {\n"
" int x;\n"
" if (false || init(&x)) {}\n"
" int b = x+1;\n"
"}";
values = tokenValues(code, "x + 1", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size());
} }
void valueFlowConditionExpressions() { void valueFlowConditionExpressions() {