Fix 10323: Wrong known value. x!=0 does not mean that x==1 (#3308)
This commit is contained in:
parent
508188df2b
commit
66956ed959
|
@ -4014,6 +4014,7 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase*
|
||||||
const Token* condTok = parenTok->astOperand2();
|
const Token* condTok = parenTok->astOperand2();
|
||||||
if (condTok->hasKnownIntValue())
|
if (condTok->hasKnownIntValue())
|
||||||
continue;
|
continue;
|
||||||
|
const bool is1 = (condTok->isComparisonOp() || condTok->tokType() == Token::eLogicalOp || astIsBool(condTok));
|
||||||
|
|
||||||
Token* startTok = blockTok;
|
Token* startTok = blockTok;
|
||||||
// Inner condition
|
// Inner condition
|
||||||
|
@ -4024,8 +4025,10 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase*
|
||||||
conds.insert(conds.end(), args.begin(), args.end());
|
conds.insert(conds.end(), args.begin(), args.end());
|
||||||
}
|
}
|
||||||
for (const Token* condTok2:conds) {
|
for (const Token* condTok2:conds) {
|
||||||
ExpressionAnalyzer a1(condTok2, makeConditionValue(1, condTok2, true), tokenlist);
|
if (is1) {
|
||||||
valueFlowGenericForward(startTok, startTok->link(), a1, settings);
|
ExpressionAnalyzer a1(condTok2, makeConditionValue(1, condTok2, true), tokenlist);
|
||||||
|
valueFlowGenericForward(startTok, startTok->link(), a1, settings);
|
||||||
|
}
|
||||||
|
|
||||||
OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(0, condTok2, true), tokenlist);
|
OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(0, condTok2, true), tokenlist);
|
||||||
valueFlowGenericForward(startTok, startTok->link(), a2, settings);
|
valueFlowGenericForward(startTok, startTok->link(), a2, settings);
|
||||||
|
@ -4045,8 +4048,10 @@ static void valueFlowConditionExpressions(TokenList *tokenlist, SymbolDatabase*
|
||||||
ExpressionAnalyzer a1(condTok2, makeConditionValue(0, condTok2, false), tokenlist);
|
ExpressionAnalyzer a1(condTok2, makeConditionValue(0, condTok2, false), tokenlist);
|
||||||
valueFlowGenericForward(startTok, startTok->link(), a1, settings);
|
valueFlowGenericForward(startTok, startTok->link(), a1, settings);
|
||||||
|
|
||||||
OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(1, condTok2, false), tokenlist);
|
if (is1) {
|
||||||
valueFlowGenericForward(startTok, startTok->link(), a2, settings);
|
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);
|
ExpressionAnalyzer a1(condTok2, makeConditionValue(0, condTok2, false), tokenlist);
|
||||||
valueFlowGenericForward(startTok->link()->next(), scope->bodyEnd, a1, settings);
|
valueFlowGenericForward(startTok->link()->next(), scope->bodyEnd, a1, settings);
|
||||||
|
|
||||||
OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(1, condTok2, false), tokenlist);
|
if (is1) {
|
||||||
valueFlowGenericForward(startTok->link()->next(), scope->bodyEnd, a2, settings);
|
OppositeExpressionAnalyzer a2(true, condTok2, makeConditionValue(1, condTok2, false), tokenlist);
|
||||||
|
valueFlowGenericForward(startTok->link()->next(), scope->bodyEnd, a2, settings);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3633,6 +3633,20 @@ private:
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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
|
// do not report both unsignedLessThanZero and knownConditionTrueFalse
|
||||||
check("void foo(unsigned int max) {\n"
|
check("void foo(unsigned int max) {\n"
|
||||||
" unsigned int num = max - 1;\n"
|
" unsigned int num = max - 1;\n"
|
||||||
|
|
Loading…
Reference in New Issue