Skip to content

Commit a66da09

Browse files
author
Jean-François DEVERGE
committed
Fix #15000: Propagate integral cast ranges with path-safe value flow
1 parent 103143d commit a66da09

33 files changed

Lines changed: 5527 additions & 587 deletions

‎.selfcheck_suppressions‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ functionStatic:*/gui/ui_*.h
4949
# --debug-warnings suppressions
5050
valueFlowBailout
5151
valueFlowBailoutIncompleteVar
52-
valueFlowMaxIterations:gui/resultstree.cpp
5352
autoNoType:externals/simplecpp/simplecpp.cpp
5453
autoNoType:cli/*.cpp
5554
autoNoType:lib/*.cpp

‎Makefile‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ $(libcppdir)/addoninfo.o: lib/addoninfo.cpp externals/picojson/picojson.h lib/ad
499499
$(libcppdir)/analyzerinfo.o: lib/analyzerinfo.cpp externals/tinyxml2/tinyxml2.h lib/analyzerinfo.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/standards.h lib/utils.h lib/xml.h
500500
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/analyzerinfo.cpp
501501

502-
$(libcppdir)/astutils.o: lib/astutils.cpp lib/astutils.h lib/check.h lib/checkclass.h lib/checkers.h lib/checkimpl.h lib/config.h lib/errortypes.h lib/findtoken.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vfvalue.h
502+
$(libcppdir)/astutils.o: lib/astutils.cpp lib/astutils.h lib/check.h lib/checkclass.h lib/checkers.h lib/checkimpl.h lib/config.h lib/errortypes.h lib/findtoken.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_common.h lib/vfvalue.h
503503
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/astutils.cpp
504504

505505
$(libcppdir)/check64bit.o: lib/check64bit.cpp lib/check.h lib/check64bit.h lib/checkers.h lib/checkimpl.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
@@ -520,7 +520,7 @@ $(libcppdir)/checkbufferoverrun.o: lib/checkbufferoverrun.cpp externals/tinyxml2
520520
$(libcppdir)/checkclass.o: lib/checkclass.cpp externals/tinyxml2/tinyxml2.h lib/astutils.h lib/check.h lib/checkclass.h lib/checkers.h lib/checkimpl.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
521521
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkclass.cpp
522522

523-
$(libcppdir)/checkcondition.o: lib/checkcondition.cpp lib/astutils.h lib/check.h lib/checkcondition.h lib/checkers.h lib/checkimpl.h lib/checkother.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
523+
$(libcppdir)/checkcondition.o: lib/checkcondition.cpp lib/astutils.h lib/check.h lib/checkcondition.h lib/checkers.h lib/checkimpl.h lib/checkother.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vf_common.h lib/vfvalue.h
524524
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkcondition.cpp
525525

526526
$(libcppdir)/checkers.o: lib/checkers.cpp lib/checkers.h lib/config.h
@@ -691,7 +691,7 @@ $(libcppdir)/vf_analyzers.o: lib/vf_analyzers.cpp lib/analyzer.h lib/astutils.h
691691
$(libcppdir)/vf_common.o: lib/vf_common.cpp lib/astutils.h lib/checkers.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
692692
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_common.cpp
693693

694-
$(libcppdir)/vf_settokenvalue.o: lib/vf_settokenvalue.cpp lib/astutils.h lib/calculate.h lib/checkers.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueflow.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
694+
$(libcppdir)/vf_settokenvalue.o: lib/vf_settokenvalue.cpp lib/astutils.h lib/calculate.h lib/checkers.h lib/config.h lib/errortypes.h lib/infer.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
695695
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_settokenvalue.cpp
696696

697697
$(libcppdir)/vfvalue.o: lib/vfvalue.cpp lib/config.h lib/errortypes.h lib/mathlib.h lib/smallvector.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vfvalue.h
@@ -961,7 +961,7 @@ test/testutils.o: test/testutils.cpp lib/check.h lib/checkers.h lib/color.h lib/
961961
test/testvaarg.o: test/testvaarg.cpp lib/check.h lib/checkers.h lib/checkimpl.h lib/checkvaarg.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
962962
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testvaarg.cpp
963963

964-
test/testvalueflow.o: test/testvalueflow.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
964+
test/testvalueflow.o: test/testvalueflow.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/infer.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueptr.h lib/vf_common.h lib/vfvalue.h test/fixture.h test/helpers.h
965965
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testvalueflow.cpp
966966

967967
test/testvarid.o: test/testvarid.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h

‎lib/astutils.cpp‎

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
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"
@@ -42,6 +43,7 @@
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+
14791532
static 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
}

‎lib/astutils.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,16 @@ const Token* getCondTokFromEnd(const Token* endBlock);
230230
const Token *findNextTokenFromBreak(const Token *breakToken);
231231

232232
/**
233-
* Extract for loop values: loopvar varid, init value, step value, last value (inclusive)
233+
* Extract for loop values: loopvar varid, init value, step value, last value (inclusive).
234+
* `initValueSource` identifies the fact supplying the minimum; `initValueWitnessSource`
235+
* identifies an actual negative point or upper bound, if present.
234236
*/
235237
bool extractForLoopValues(const Token *forToken,
236238
nonneg int &varid,
237239
bool &knownInitValue,
238240
MathLib::bigint &initValue,
241+
const ValueFlow::Value*& initValueSource,
242+
const ValueFlow::Value*& initValueWitnessSource,
239243
bool &partialCond,
240244
MathLib::bigint &stepValue,
241245
MathLib::bigint &lastValue);

‎lib/checkbufferoverrun.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ void CheckBufferOverrunImpl::arrayIndex()
363363
arrayIndexError(tok, dimensions, indexValues);
364364
}
365365

366+
366367
// Negative index
367368
bool neg = false;
368369
std::vector<ValueFlow::Value> negativeIndexes;

0 commit comments

Comments
 (0)