From 66956ed9595a9cbfeb7aad6737fd79cda8f40d0e Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sat, 26 Jun 2021 02:16:45 -0500 Subject: [PATCH] Fix 10323: Wrong known value. x!=0 does not mean that x==1 (#3308) --- lib/valueflow.cpp | 19 +++++++++++++------ test/testcondition.cpp | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index cc41acc82..9e7581502 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4014,6 +4014,7 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase* const Token* condTok = parenTok->astOperand2(); if (condTok->hasKnownIntValue()) continue; + const bool is1 = (condTok->isComparisonOp() || condTok->tokType() == Token::eLogicalOp || astIsBool(condTok)); Token* startTok = blockTok; // Inner condition @@ -4024,8 +4025,10 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase* conds.insert(conds.end(), args.begin(), args.end()); } for (const Token* condTok2:conds) { - ExpressionAnalyzer a1(condTok2, makeConditionValue(1, condTok2, true), tokenlist); - valueFlowGenericForward(startTok, startTok->link(), a1, settings); + if (is1) { + ExpressionAnalyzer a1(condTok2, makeConditionValue(1, condTok2, true), tokenlist); + valueFlowGenericForward(startTok, startTok->link(), a1, settings); + } OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(0, condTok2, true), tokenlist); valueFlowGenericForward(startTok, startTok->link(), a2, settings); @@ -4045,8 +4048,10 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase* ExpressionAnalyzer a1(condTok2, makeConditionValue(0, condTok2, false), tokenlist); valueFlowGenericForward(startTok, startTok->link(), a1, settings); - OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(1, condTok2, false), tokenlist); - valueFlowGenericForward(startTok, startTok->link(), a2, settings); + if (is1) { + OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(1, condTok2, false), tokenlist); + valueFlowGenericForward(startTok, startTok->link(), a2, settings); + } } } @@ -4056,8 +4061,10 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase* ExpressionAnalyzer a1(condTok2, makeConditionValue(0, condTok2, false), tokenlist); valueFlowGenericForward(startTok->link()->next(), scope->bodyEnd, a1, settings); - OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(1, condTok2, false), tokenlist); - valueFlowGenericForward(startTok->link()->next(), scope->bodyEnd, a2, settings); + if (is1) { + OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(1, condTok2, false), tokenlist); + valueFlowGenericForward(startTok->link()->next(), scope->bodyEnd, a2, settings); + } } } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 7af5d107f..ae499cf50 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -3633,6 +3633,20 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + // #10323 + check("void foo(int x) {\n" + " if(x)\n" + " if(x == 1) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void foo(int x) {\n" + " if(x) {}\n" + " else\n" + " if(x == 1) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (style) Condition 'x==1' is always false\n", errout.str()); + // do not report both unsignedLessThanZero and knownConditionTrueFalse check("void foo(unsigned int max) {\n" " unsigned int num = max - 1;\n"