3333#include " utils.h"
3434#include " valueflow.h"
3535#include " valueptr.h"
36+ #include " vf_common.h"
3637#include " vfvalue.h"
3738
3839#include " checkclass.h"
4243#include < functional>
4344#include < initializer_list>
4445#include < iterator>
46+ #include < limits>
4547#include < list>
4648#include < set>
4749#include < type_traits>
@@ -956,29 +958,58 @@ bool extractForLoopValues(const Token *forToken,
956958 nonneg int &varid,
957959 bool &knownInitValue,
958960 MathLib::bigint &initValue,
961+ const ValueFlow::Value*& initValueSource,
962+ const ValueFlow::Value*& initValueWitnessSource,
959963 bool &partialCond,
960964 MathLib::bigint &stepValue,
961965 MathLib::bigint &lastValue)
962966{
967+ initValueSource = nullptr ;
968+ initValueWitnessSource = nullptr ;
963969 if (!Token::simpleMatch (forToken, " for (" ) || !Token::simpleMatch (forToken->next ()->astOperand2 (), " ;" ))
964970 return false ;
965971 const Token *initExpr = forToken->next ()->astOperand2 ()->astOperand1 ();
966972 const Token *condExpr = forToken->next ()->astOperand2 ()->astOperand2 ()->astOperand1 ();
967973 const Token *incExpr = forToken->next ()->astOperand2 ()->astOperand2 ()->astOperand2 ();
968974 if (!initExpr || !initExpr->isBinaryOp () || initExpr->str () != " =" || !Token::Match (initExpr->astOperand1 (), " %var%" ))
969975 return false ;
970- std::vector<MathLib::bigint> minInitValue =
971- getMinValue (makeIntegralInferModel (), initExpr->astOperand2 ()->values ());
976+ const Token* const initToken = initExpr->astOperand2 ();
977+ const std::vector<MathLib::bigint> minInitValue =
978+ getMinValue (makeIntegralInferModel (), initToken->values (), &initValueSource);
972979 if (minInitValue.empty ()) {
973- const ValueFlow::Value* v = initExpr->astOperand2 ()->getMinValue (true );
974- if (v)
975- minInitValue.push_back (v->intvalue );
980+ knownInitValue = false ;
981+ initValue = 0 ;
982+ } else {
983+ knownInitValue = initToken->hasKnownIntValue ();
984+ initValue = minInitValue.front ();
985+ if (knownInitValue && initValueSource && initValueSource->bound != ValueFlow::Value::Bound::Point) {
986+ const auto it = std::find_if (initToken->values ().cbegin (),
987+ initToken->values ().cend (),
988+ [initValue](const ValueFlow::Value& value) {
989+ return value.isIntValue () && !value.isImpossible () && !value.isInconclusive () &&
990+ value.bound == ValueFlow::Value::Bound::Point && value.intvalue == initValue;
991+ });
992+ if (it != initToken->values ().cend ())
993+ initValueSource = &*it;
994+ }
995+ }
996+ const auto isNegativeEvidence = [](const ValueFlow::Value& candidate) {
997+ return candidate.isIntValue () && !candidate.isImpossible () && !candidate.isInconclusive () &&
998+ (candidate.isKnown () || candidate.isPossible ()) && candidate.path >= 0 && candidate.intvalue < 0 &&
999+ (candidate.bound == ValueFlow::Value::Bound::Point || candidate.bound == ValueFlow::Value::Bound::Upper);
1000+ };
1001+ if (initValueSource && isNegativeEvidence (*initValueSource)) {
1002+ initValueWitnessSource = initValueSource;
1003+ } else {
1004+ const auto it = std::find_if (initToken->values ().cbegin (),
1005+ initToken->values ().cend (),
1006+ isNegativeEvidence);
1007+ if (it != initToken->values ().cend ())
1008+ initValueWitnessSource = &*it;
9761009 }
977- if (minInitValue.empty ())
1010+ if (minInitValue.empty () && !initValueWitnessSource )
9781011 return false ;
9791012 varid = initExpr->astOperand1 ()->varId ();
980- knownInitValue = initExpr->astOperand2 ()->hasKnownIntValue ();
981- initValue = minInitValue.front ();
9821013 partialCond = Token::Match (condExpr, " %oror%|&&" );
9831014 visitAstNodes (condExpr, [varid, &condExpr](const Token *tok) {
9841015 if (Token::Match (tok, " %oror%|&&" ))
@@ -994,10 +1025,14 @@ bool extractForLoopValues(const Token *forToken,
9941025 if (!incExpr || !incExpr->isUnaryOp (" ++" ) || incExpr->astOperand1 ()->varId () != varid)
9951026 return false ;
9961027 stepValue = 1 ;
997- if (condExpr->str () == " <" )
998- lastValue = condExpr->astOperand2 ()->getKnownIntValue () - 1 ;
999- else
1028+ if (condExpr->str () == " <" ) {
1029+ const MathLib::bigint limit = condExpr->astOperand2 ()->getKnownIntValue ();
1030+ if (limit == std::numeric_limits<MathLib::bigint>::min ())
1031+ return false ;
1032+ lastValue = limit - 1 ;
1033+ } else {
10001034 lastValue = condExpr->astOperand2 ()->getKnownIntValue ();
1035+ }
10011036 return true ;
10021037}
10031038
@@ -1476,6 +1511,24 @@ static inline bool isDifferentKnownValues(const Token * const tok1, const Token
14761511 });
14771512}
14781513
1514+ // Like isDifferentKnownValues(), but the two constants are read as they compare against 'exprTok'.
1515+ static bool isDifferentKnownValues (const Token* tok1, const Token* tok2, const Token* exprTok, const Settings& settings)
1516+ {
1517+ return compareKnownValue (tok1, tok2, [&](const ValueFlow::Value& v1, const ValueFlow::Value& v2, bool sameLifetime) {
1518+ if (!v1.isIntValue () || !v2.isIntValue ()) {
1519+ bool equal = v1.equalValue (v2);
1520+ if (v1.isIteratorValue ())
1521+ equal &= sameLifetime;
1522+ return !equal;
1523+ }
1524+ MathLib::bigint value1 = v1.intvalue ;
1525+ MathLib::bigint value2 = v2.intvalue ;
1526+ ValueFlow::castConstantToOperandType (tok1, exprTok, value1, settings);
1527+ ValueFlow::castConstantToOperandType (tok2, exprTok, value2, settings);
1528+ return value1 != value2;
1529+ });
1530+ }
1531+
14791532static inline bool isSameConstantValue (bool macro, const Token* tok1, const Token* tok2)
14801533{
14811534 if (tok1 == nullptr || tok2 == nullptr )
@@ -1903,9 +1956,9 @@ bool isOppositeCond(bool isNot, const Token * const cond1, const Token * const c
19031956 if (!isNot) {
19041957 if (cond1->str () == " ==" && cond2->str () == " ==" ) {
19051958 if (isSameExpression (true , cond1->astOperand1 (), cond2->astOperand1 (), settings, pure, followVar, errors))
1906- return isDifferentKnownValues (cond1->astOperand2 (), cond2->astOperand2 ());
1959+ return isDifferentKnownValues (cond1->astOperand2 (), cond2->astOperand2 (), cond1-> astOperand1 (), settings );
19071960 if (isSameExpression (true , cond1->astOperand2 (), cond2->astOperand2 (), settings, pure, followVar, errors))
1908- return isDifferentKnownValues (cond1->astOperand1 (), cond2->astOperand1 ());
1961+ return isDifferentKnownValues (cond1->astOperand1 (), cond2->astOperand1 (), cond1-> astOperand2 (), settings );
19091962 }
19101963
19111964 if (Library::isContainerYield (cond1, Library::Container::Yield::EMPTY , " empty" ) &&
@@ -2011,10 +2064,18 @@ bool isOppositeCond(bool isNot, const Token * const cond1, const Token * const c
20112064 const ValueFlow::Value &rhsValue1 = value1->values ().front ();
20122065 const ValueFlow::Value &rhsValue2 = value2->values ().front ();
20132066
2067+ // The operands are compared in their common type. When one side is an unsigned constant and the
2068+ // other a signed expression, the comparison happens in the unsigned domain, so the constant must be
2069+ // read as that expression's type would see it: 0xFFFFFFFFu is -1 for an 'int' operand.
2070+ MathLib::bigint bound1 = rhsValue1.intvalue ;
2071+ MathLib::bigint bound2 = rhsValue2.intvalue ;
2072+ ValueFlow::castConstantToOperandType (value1, expr1, bound1, settings);
2073+ ValueFlow::castConstantToOperandType (value2, expr2, bound2, settings);
2074+
20142075 if (op1 == " <" || op1 == " <=" )
2015- return (op2 == " ==" || op2 == " >" || op2 == " >=" ) && (rhsValue1. intvalue < rhsValue2. intvalue );
2076+ return (op2 == " ==" || op2 == " >" || op2 == " >=" ) && (bound1 < bound2 );
20162077 if (op1 == " >=" || op1 == " >" )
2017- return (op2 == " ==" || op2 == " <" || op2 == " <=" ) && (rhsValue1. intvalue > rhsValue2. intvalue );
2078+ return (op2 == " ==" || op2 == " <" || op2 == " <=" ) && (bound1 > bound2 );
20182079
20192080 return false ;
20202081 }
0 commit comments