Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .selfcheck_suppressions
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ functionStatic:*/gui/ui_*.h
# --debug-warnings suppressions
valueFlowBailout
valueFlowBailoutIncompleteVar
valueFlowMaxIterations:gui/resultstree.cpp
autoNoType:externals/simplecpp/simplecpp.cpp
autoNoType:cli/*.cpp
autoNoType:lib/*.cpp
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ $(libcppdir)/addoninfo.o: lib/addoninfo.cpp externals/picojson/picojson.h lib/ad
$(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
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/analyzerinfo.cpp

$(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
$(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
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/astutils.cpp

$(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
Expand All @@ -520,7 +520,7 @@ $(libcppdir)/checkbufferoverrun.o: lib/checkbufferoverrun.cpp externals/tinyxml2
$(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
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkclass.cpp

$(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
$(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
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkcondition.cpp

$(libcppdir)/checkers.o: lib/checkers.cpp lib/checkers.h lib/config.h
Expand Down Expand Up @@ -691,7 +691,7 @@ $(libcppdir)/vf_analyzers.o: lib/vf_analyzers.cpp lib/analyzer.h lib/astutils.h
$(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
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_common.cpp

$(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
$(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
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/vf_settokenvalue.cpp

$(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
Expand Down Expand Up @@ -961,7 +961,7 @@ test/testutils.o: test/testutils.cpp lib/check.h lib/checkers.h lib/color.h lib/
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
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testvaarg.cpp

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
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
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testvalueflow.cpp

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
Expand Down
91 changes: 76 additions & 15 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "utils.h"
#include "valueflow.h"
#include "valueptr.h"
#include "vf_common.h"
#include "vfvalue.h"

#include "checkclass.h"
Expand All @@ -42,6 +43,7 @@
#include <functional>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <list>
#include <set>
#include <type_traits>
Expand Down Expand Up @@ -956,29 +958,58 @@ bool extractForLoopValues(const Token *forToken,
nonneg int &varid,
bool &knownInitValue,
MathLib::bigint &initValue,
const ValueFlow::Value*& initValueSource,
const ValueFlow::Value*& initValueWitnessSource,
bool &partialCond,
MathLib::bigint &stepValue,
MathLib::bigint &lastValue)
{
initValueSource = nullptr;
initValueWitnessSource = nullptr;
if (!Token::simpleMatch(forToken, "for (") || !Token::simpleMatch(forToken->next()->astOperand2(), ";"))
return false;
const Token *initExpr = forToken->next()->astOperand2()->astOperand1();
const Token *condExpr = forToken->next()->astOperand2()->astOperand2()->astOperand1();
const Token *incExpr = forToken->next()->astOperand2()->astOperand2()->astOperand2();
if (!initExpr || !initExpr->isBinaryOp() || initExpr->str() != "=" || !Token::Match(initExpr->astOperand1(), "%var%"))
return false;
std::vector<MathLib::bigint> minInitValue =
getMinValue(makeIntegralInferModel(), initExpr->astOperand2()->values());
const Token* const initToken = initExpr->astOperand2();
const std::vector<MathLib::bigint> minInitValue =
getMinValue(makeIntegralInferModel(), initToken->values(), &initValueSource);
if (minInitValue.empty()) {
const ValueFlow::Value* v = initExpr->astOperand2()->getMinValue(true);
if (v)
minInitValue.push_back(v->intvalue);
knownInitValue = false;
initValue = 0;
} else {
knownInitValue = initToken->hasKnownIntValue();
initValue = minInitValue.front();
if (knownInitValue && initValueSource && initValueSource->bound != ValueFlow::Value::Bound::Point) {
const auto it = std::find_if(initToken->values().cbegin(),
initToken->values().cend(),
[initValue](const ValueFlow::Value& value) {
return value.isIntValue() && !value.isImpossible() && !value.isInconclusive() &&
value.bound == ValueFlow::Value::Bound::Point && value.intvalue == initValue;
});
if (it != initToken->values().cend())
initValueSource = &*it;
}
}
const auto isNegativeEvidence = [](const ValueFlow::Value& candidate) {
return candidate.isIntValue() && !candidate.isImpossible() && !candidate.isInconclusive() &&
(candidate.isKnown() || candidate.isPossible()) && candidate.path >= 0 && candidate.intvalue < 0 &&
(candidate.bound == ValueFlow::Value::Bound::Point || candidate.bound == ValueFlow::Value::Bound::Upper);
};
if (initValueSource && isNegativeEvidence(*initValueSource)) {
initValueWitnessSource = initValueSource;
} else {
const auto it = std::find_if(initToken->values().cbegin(),
initToken->values().cend(),
isNegativeEvidence);
if (it != initToken->values().cend())
initValueWitnessSource = &*it;
}
if (minInitValue.empty())
if (minInitValue.empty() && !initValueWitnessSource)
return false;
varid = initExpr->astOperand1()->varId();
knownInitValue = initExpr->astOperand2()->hasKnownIntValue();
initValue = minInitValue.front();
partialCond = Token::Match(condExpr, "%oror%|&&");
visitAstNodes(condExpr, [varid, &condExpr](const Token *tok) {
if (Token::Match(tok, "%oror%|&&"))
Expand All @@ -994,10 +1025,14 @@ bool extractForLoopValues(const Token *forToken,
if (!incExpr || !incExpr->isUnaryOp("++") || incExpr->astOperand1()->varId() != varid)
return false;
stepValue = 1;
if (condExpr->str() == "<")
lastValue = condExpr->astOperand2()->getKnownIntValue() - 1;
else
if (condExpr->str() == "<") {
const MathLib::bigint limit = condExpr->astOperand2()->getKnownIntValue();
if (limit == std::numeric_limits<MathLib::bigint>::min())
return false;
lastValue = limit - 1;
} else {
lastValue = condExpr->astOperand2()->getKnownIntValue();
}
return true;
}

Expand Down Expand Up @@ -1476,6 +1511,24 @@ static inline bool isDifferentKnownValues(const Token * const tok1, const Token
});
}

// Like isDifferentKnownValues(), but the two constants are read as they compare against 'exprTok'.
static bool isDifferentKnownValues(const Token* tok1, const Token* tok2, const Token* exprTok, const Settings& settings)
{
return compareKnownValue(tok1, tok2, [&](const ValueFlow::Value& v1, const ValueFlow::Value& v2, bool sameLifetime) {
if (!v1.isIntValue() || !v2.isIntValue()) {
bool equal = v1.equalValue(v2);
if (v1.isIteratorValue())
equal &= sameLifetime;
return !equal;
}
MathLib::bigint value1 = v1.intvalue;
MathLib::bigint value2 = v2.intvalue;
ValueFlow::castConstantToOperandType(tok1, exprTok, value1, settings);
ValueFlow::castConstantToOperandType(tok2, exprTok, value2, settings);
return value1 != value2;
});
}

static inline bool isSameConstantValue(bool macro, const Token* tok1, const Token* tok2)
{
if (tok1 == nullptr || tok2 == nullptr)
Expand Down Expand Up @@ -1903,9 +1956,9 @@ bool isOppositeCond(bool isNot, const Token * const cond1, const Token * const c
if (!isNot) {
if (cond1->str() == "==" && cond2->str() == "==") {
if (isSameExpression(true, cond1->astOperand1(), cond2->astOperand1(), settings, pure, followVar, errors))
return isDifferentKnownValues(cond1->astOperand2(), cond2->astOperand2());
return isDifferentKnownValues(cond1->astOperand2(), cond2->astOperand2(), cond1->astOperand1(), settings);
if (isSameExpression(true, cond1->astOperand2(), cond2->astOperand2(), settings, pure, followVar, errors))
return isDifferentKnownValues(cond1->astOperand1(), cond2->astOperand1());
return isDifferentKnownValues(cond1->astOperand1(), cond2->astOperand1(), cond1->astOperand2(), settings);
}

if (Library::isContainerYield(cond1, Library::Container::Yield::EMPTY, "empty") &&
Expand Down Expand Up @@ -2011,10 +2064,18 @@ bool isOppositeCond(bool isNot, const Token * const cond1, const Token * const c
const ValueFlow::Value &rhsValue1 = value1->values().front();
const ValueFlow::Value &rhsValue2 = value2->values().front();

// The operands are compared in their common type. When one side is an unsigned constant and the
// other a signed expression, the comparison happens in the unsigned domain, so the constant must be
// read as that expression's type would see it: 0xFFFFFFFFu is -1 for an 'int' operand.
MathLib::bigint bound1 = rhsValue1.intvalue;
MathLib::bigint bound2 = rhsValue2.intvalue;
ValueFlow::castConstantToOperandType(value1, expr1, bound1, settings);
ValueFlow::castConstantToOperandType(value2, expr2, bound2, settings);

if (op1 == "<" || op1 == "<=")
return (op2 == "==" || op2 == ">" || op2 == ">=") && (rhsValue1.intvalue < rhsValue2.intvalue);
return (op2 == "==" || op2 == ">" || op2 == ">=") && (bound1 < bound2);
if (op1 == ">=" || op1 == ">")
return (op2 == "==" || op2 == "<" || op2 == "<=") && (rhsValue1.intvalue > rhsValue2.intvalue);
return (op2 == "==" || op2 == "<" || op2 == "<=") && (bound1 > bound2);

return false;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,16 @@ const Token* getCondTokFromEnd(const Token* endBlock);
const Token *findNextTokenFromBreak(const Token *breakToken);

/**
* Extract for loop values: loopvar varid, init value, step value, last value (inclusive)
* Extract for loop values: loopvar varid, init value, step value, last value (inclusive).
* `initValueSource` identifies the fact supplying the minimum; `initValueWitnessSource`
* identifies an actual negative point or upper bound, if present.
*/
bool extractForLoopValues(const Token *forToken,
nonneg int &varid,
bool &knownInitValue,
MathLib::bigint &initValue,
const ValueFlow::Value*& initValueSource,
const ValueFlow::Value*& initValueWitnessSource,
bool &partialCond,
MathLib::bigint &stepValue,
MathLib::bigint &lastValue);
Expand Down
1 change: 1 addition & 0 deletions lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ void CheckBufferOverrunImpl::arrayIndex()
arrayIndexError(tok, dimensions, indexValues);
}


// Negative index
bool neg = false;
std::vector<ValueFlow::Value> negativeIndexes;
Expand Down
Loading
Loading