Skip to content

Commit 38b2c5e

Browse files
authored
Fix #11777 (False positive: uninitialized variable, handling 'false ||' in valueflow) (cppcheck-opensource#5169)
1 parent 082331c commit 38b2c5e

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

lib/valueflow.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7852,14 +7852,14 @@ bool findTokenSkipDeadCodeImpl(const Library* library, Token* start, const Token
78527852
tok = thenStart->link();
78537853
}
78547854
} else if (Token::Match(tok->astParent(), "&&|?|%oror%") && astIsLHS(tok) && tok->hasKnownIntValue()) {
7855-
int r = tok->values().front().intvalue;
7855+
const bool cond = tok->values().front().intvalue != 0;
78567856
Token* next = nullptr;
7857-
if ((r == 0 && Token::simpleMatch(tok->astParent(), "||")) ||
7858-
(r != 0 && Token::simpleMatch(tok->astParent(), "&&"))) {
7857+
if ((cond && Token::simpleMatch(tok->astParent(), "||")) ||
7858+
(!cond && Token::simpleMatch(tok->astParent(), "&&"))) {
78597859
next = nextAfterAstRightmostLeaf(tok->astParent());
78607860
} else if (Token::simpleMatch(tok->astParent(), "?")) {
78617861
Token* colon = tok->astParent()->astOperand2();
7862-
if (r == 0) {
7862+
if (!cond) {
78637863
next = colon;
78647864
} else {
78657865
if (findTokenSkipDeadCodeImpl(library, tok->astParent()->next(), colon, pred, found))

test/testvalueflow.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5554,6 +5554,17 @@ class TestValueFlow : public TestFixture {
55545554
"}\n";
55555555
values = tokenValues(code, ". id", ValueFlow::Value::ValueType::UNINIT);
55565556
ASSERT_EQUALS(0, values.size());
5557+
5558+
// #11777 - false || ...
5559+
code = "bool init(int *p);\n"
5560+
"\n"
5561+
"void uninitvar_FP9() {\n"
5562+
" int x;\n"
5563+
" if (false || init(&x)) {}\n"
5564+
" int b = x+1;\n"
5565+
"}";
5566+
values = tokenValues(code, "x + 1", ValueFlow::Value::ValueType::UNINIT);
5567+
ASSERT_EQUALS(0, values.size());
55575568
}
55585569

55595570
void valueFlowConditionExpressions() {

0 commit comments

Comments
 (0)