Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 9979: false positive: containerOutOfBounds with conditional resize #3136

Merged
merged 27 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
82810ed
Fix issue 9979: false positive: containerOutOfBounds with conditional…
pfultz2 Feb 18, 2021
e1375b8
Remove commented out code
pfultz2 Feb 18, 2021
bc9ebee
Add more tests
pfultz2 Feb 18, 2021
2a3e9ba
Remove adjacent values
pfultz2 Feb 19, 2021
b0f298b
Dont truncate impossible values
pfultz2 Feb 19, 2021
07ee205
Fix some tests
pfultz2 Feb 19, 2021
75e8916
Fix bug in removing contradictions
pfultz2 Feb 19, 2021
d7d4c44
Fix more bugs in removing contradictions
pfultz2 Feb 20, 2021
56a1408
Skip literals
pfultz2 Feb 20, 2021
c9a2d1a
Flip assert
pfultz2 Feb 20, 2021
680a4c6
Fix valueflow tests
pfultz2 Feb 20, 2021
64e3b01
Dont set literals
pfultz2 Feb 20, 2021
31b7067
Format
pfultz2 Feb 20, 2021
9a22bc8
Remove unnecessary headers
pfultz2 Feb 20, 2021
0ef3651
Fix uninitialized variables
pfultz2 Feb 22, 2021
649cb59
Merge
pfultz2 Feb 22, 2021
a53726d
Prevent UB in calculate function
pfultz2 Feb 22, 2021
64fd616
Fix cppcheck warning
pfultz2 Feb 22, 2021
305f03a
Merge branch 'main' into valueflow-infer-unsigned-compare
pfultz2 Feb 24, 2021
ad89530
Check value type
pfultz2 Feb 24, 2021
65f4519
Format
pfultz2 Feb 24, 2021
87e38aa
Merge branch 'main' into valueflow-infer-unsigned-compare
pfultz2 Feb 25, 2021
b3af86c
Make test todo
pfultz2 Feb 28, 2021
62d7377
Merge branch 'main' into valueflow-infer-unsigned-compare
pfultz2 Mar 1, 2021
0377a41
Remove else
pfultz2 Mar 30, 2021
c8fcd31
Merge branch 'main' into valueflow-infer-unsigned-compare
pfultz2 Mar 30, 2021
0ccd789
Rerun dmake
pfultz2 Mar 30, 2021
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
Prev Previous commit
Next Next commit
Format
  • Loading branch information
pfultz2 committed Feb 20, 2021
commit 31b70677018854e4275657b0589e31b762f989c7
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ bool astIsIntegral(const Token *tok, bool unknown)
return vt->isIntegral() && vt->pointer == 0U;
}

bool astIsUnsigned(const Token *tok)
bool astIsUnsigned(const Token* tok)
{
return tok && tok->valueType() && tok->valueType()->sign == ValueType::UNSIGNED;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bool astIsSignedChar(const Token *tok);
bool astIsUnknownSignChar(const Token *tok);
/** Is expression of integral type? */
bool astIsIntegral(const Token *tok, bool unknown);
bool astIsUnsigned(const Token *tok);
bool astIsUnsigned(const Token* tok);
/** Is expression of floating point type? */
bool astIsFloat(const Token *tok, bool unknown);
/** Is expression of boolean type? */
Expand Down
15 changes: 4 additions & 11 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2024,23 +2024,18 @@ static bool removeContradiction(std::list<ValueFlow::Value>& values)
return result;
}


using ValueIterator = std::list<ValueFlow::Value>::iterator;

template<class Iterator>
template <class Iterator>
static ValueIterator removeAdjacentValues(std::list<ValueFlow::Value>& values, ValueIterator x, Iterator start, Iterator last)
{
if (!isAdjacent(*x, **start))
return std::next(x);
auto it = std::adjacent_find(start, last, [](ValueIterator x, ValueIterator y) {
return !isAdjacent(*x, *y);
});
auto it = std::adjacent_find(start, last, [](ValueIterator x, ValueIterator y) { return !isAdjacent(*x, *y); });
if (it == last)
it--;
(*it)->bound = x->bound;
std::for_each(start, it, [&](ValueIterator y) {
values.erase(y);
});
std::for_each(start, it, [&](ValueIterator y) { values.erase(y); });
return values.erase(x);
}

Expand All @@ -2056,7 +2051,7 @@ static void mergeAdjacent(std::list<ValueFlow::Value>& values)
continue;
}
std::vector<ValueIterator> adjValues;
for (auto y = values.begin(); y != values.end();y++) {
for (auto y = values.begin(); y != values.end(); y++) {
if (x == y)
continue;
if (y->isNonValue())
Expand Down Expand Up @@ -2090,7 +2085,6 @@ static void mergeAdjacent(std::list<ValueFlow::Value>& values)
x = removeAdjacentValues(values, x, adjValues.rbegin(), adjValues.rend());
else if (x->bound == ValueFlow::Value::Bound::Upper)
x = removeAdjacentValues(values, x, adjValues.begin(), adjValues.end());

}
}

Expand Down Expand Up @@ -2132,7 +2126,6 @@ static void removeContradictions(std::list<ValueFlow::Value>& values)
}
}


bool Token::addValue(const ValueFlow::Value &value)
{
if (value.isKnown() && mImpl->mValues) {
Expand Down
12 changes: 6 additions & 6 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1455,9 +1455,9 @@ static std::vector<MathLib::bigint> minUnsignedValue(const Token* tok, int depth
return result;
}

static void valueFlowImpossibleValues(TokenList *tokenList, const Settings* settings)
static void valueFlowImpossibleValues(TokenList* tokenList, const Settings* settings)
{
for (Token *tok = tokenList->front(); tok; tok = tok->next()) {
for (Token* tok = tokenList->front(); tok; tok = tok->next()) {
if (tok->hasKnownIntValue())
continue;
if (astIsUnsigned(tok)) {
Expand Down Expand Up @@ -4749,8 +4749,8 @@ ValueFlow::Value inferCondition(const std::string& op, const Token* varTok, Math
if (varTok->hasKnownIntValue())
return ValueFlow::Value{};
if (std::none_of(varTok->values().begin(), varTok->values().end(), [](const ValueFlow::Value& v) {
return v.isImpossible() && v.valueType == ValueFlow::Value::ValueType::INT;
})) {
return v.isImpossible() && v.valueType == ValueFlow::Value::ValueType::INT;
})) {
return ValueFlow::Value{};
}
const ValueFlow::Value* result = nullptr;
Expand Down Expand Up @@ -4818,11 +4818,11 @@ static void valueFlowInferCondition(TokenList* tokenlist,
ValueFlow::Value value{};
std::string op = tok->str();
if (tok->astOperand1()->hasKnownIntValue()) {
MathLib::bigint val = tok->astOperand1()->values().front().intvalue;
MathLib::bigint val = tok->astOperand1()->values().front().intvalue;
const Token* varTok = tok->astOperand2();
value = inferCondition(tok->str(), val, varTok);
} else if (tok->astOperand2()->hasKnownIntValue()) {
MathLib::bigint val = tok->astOperand2()->values().front().intvalue;
MathLib::bigint val = tok->astOperand2()->values().front().intvalue;
const Token* varTok = tok->astOperand1();
value = inferCondition(tok->str(), varTok, val);
}
Expand Down
30 changes: 18 additions & 12 deletions lib/valueflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ namespace ValueFlow {
};

struct less {
template<class T, class U>
bool operator()(const T& x, const U& y) const {
template <class T, class U>
bool operator()(const T& x, const U& y) const
{
return x < y;
}
};

struct adjacent {
template<class T, class U>
bool operator()(const T& x, const U& y) const {
template <class T, class U>
bool operator()(const T& x, const U& y) const
{
return std::abs(x - y) == 1;
}
};
Expand Down Expand Up @@ -160,23 +162,27 @@ namespace ValueFlow {

struct compareVisitor {
struct innerVisitor {
template<class Compare, class T, class U>
void operator()(bool& result, Compare compare, T x, U y) const {
template <class Compare, class T, class U>
void operator()(bool& result, Compare compare, T x, U y) const
{
result = compare(x, y);
}
};
template<class Compare, class T>
void operator()(bool& result, const Value& rhs, Compare compare, T x) const {
visitValue(rhs, std::bind(innerVisitor{}, std::ref(result), std::move(compare), x, std::placeholders::_1));
template <class Compare, class T>
void operator()(bool& result, const Value& rhs, Compare compare, T x) const
{
visitValue(rhs,
std::bind(innerVisitor{}, std::ref(result), std::move(compare), x, std::placeholders::_1));
}

};

template<class Compare>
template <class Compare>
bool compareValue(const Value& rhs, Compare compare) const
{
bool result = false;
visitValue(*this, std::bind(compareVisitor{}, std::ref(result), std::ref(rhs), std::move(compare), std::placeholders::_1));
visitValue(
*this,
std::bind(compareVisitor{}, std::ref(result), std::ref(rhs), std::move(compare), std::placeholders::_1));
return result;
}

Expand Down
26 changes: 15 additions & 11 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1718,23 +1718,27 @@ class TestCondition : public TestFixture {
" b5 = a % 5 >= 5;\n"
" return a % 5 > 5;\n"
"}");
ASSERT_EQUALS("[test.cpp:7]: (style) Condition 'a%5>5' is always false\n"
"[test.cpp:2]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:3]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:4]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:5]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:6]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:7]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n", errout.str());
ASSERT_EQUALS(
"[test.cpp:7]: (style) Condition 'a%5>5' is always false\n"
"[test.cpp:2]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:3]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:4]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:5]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:6]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:7]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n",
errout.str());

check("void f(bool& b1, bool& b2) {\n"
" b1 = bar() % 5 < 889;\n"
" if(x[593] % 5 <= 5)\n"
" b2 = x.a % 5 == 5;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'x[593]%5<=5' is always true\n"
"[test.cpp:2]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:3]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:4]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n", errout.str());
ASSERT_EQUALS(
"[test.cpp:3]: (style) Condition 'x[593]%5<=5' is always true\n"
"[test.cpp:2]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:3]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:4]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n",
errout.str());

check("void f() {\n"
" if (a % 2 + b % 2 == 2)\n"
Expand Down
3 changes: 2 additions & 1 deletion test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ class TestValueFlow : public TestFixture {
ASSERT_EQUALS(0, valueOfTok("x(NULL);", "NULL").intvalue);
ASSERT_EQUALS((int)('a'), valueOfTok("x='a';", "'a'").intvalue);
ASSERT_EQUALS((int)('\n'), valueOfTok("x='\\n';", "'\\n'").intvalue);
TODO_ASSERT_EQUALS(0xFFFFFFFF00000000, -1, valueOfTok("x=0xFFFFFFFF00000000;","0xFFFFFFFF00000000").intvalue); // #7701
TODO_ASSERT_EQUALS(
0xFFFFFFFF00000000, -1, valueOfTok("x=0xFFFFFFFF00000000;", "0xFFFFFFFF00000000").intvalue); // #7701

// scope
{
Expand Down