From 1a127e93f3fdd2ede7752663612a2969e781a4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 17 Jun 2023 20:18:18 +0200 Subject: [PATCH] Fix #11777 (False positive: uninitialized variable, handling 'false ||' in valueflow) --- lib/valueflow.cpp | 8 ++++---- test/testvalueflow.cpp | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 1c0071e1a80..ad778b72283 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -7852,14 +7852,14 @@ bool findTokenSkipDeadCodeImpl(const Library* library, Token* start, const Token tok = thenStart->link(); } } 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; - if ((r == 0 && Token::simpleMatch(tok->astParent(), "||")) || - (r != 0 && Token::simpleMatch(tok->astParent(), "&&"))) { + if ((cond && Token::simpleMatch(tok->astParent(), "||")) || + (!cond && Token::simpleMatch(tok->astParent(), "&&"))) { next = nextAfterAstRightmostLeaf(tok->astParent()); } else if (Token::simpleMatch(tok->astParent(), "?")) { Token* colon = tok->astParent()->astOperand2(); - if (r == 0) { + if (!cond) { next = colon; } else { if (findTokenSkipDeadCodeImpl(library, tok->astParent()->next(), colon, pred, found)) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 1645a92cf71..38c9f39afb5 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -5554,6 +5554,17 @@ class TestValueFlow : public TestFixture { "}\n"; values = tokenValues(code, ". id", ValueFlow::Value::ValueType::UNINIT); 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() {