-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
danmar
merged 27 commits into
danmar:main
from
pfultz2:valueflow-infer-unsigned-compare
Mar 30, 2021
Merged
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 e1375b8
Remove commented out code
pfultz2 bc9ebee
Add more tests
pfultz2 2a3e9ba
Remove adjacent values
pfultz2 b0f298b
Dont truncate impossible values
pfultz2 07ee205
Fix some tests
pfultz2 75e8916
Fix bug in removing contradictions
pfultz2 d7d4c44
Fix more bugs in removing contradictions
pfultz2 56a1408
Skip literals
pfultz2 c9a2d1a
Flip assert
pfultz2 680a4c6
Fix valueflow tests
pfultz2 64e3b01
Dont set literals
pfultz2 31b7067
Format
pfultz2 9a22bc8
Remove unnecessary headers
pfultz2 0ef3651
Fix uninitialized variables
pfultz2 649cb59
Merge
pfultz2 a53726d
Prevent UB in calculate function
pfultz2 64fd616
Fix cppcheck warning
pfultz2 305f03a
Merge branch 'main' into valueflow-infer-unsigned-compare
pfultz2 ad89530
Check value type
pfultz2 65f4519
Format
pfultz2 87e38aa
Merge branch 'main' into valueflow-infer-unsigned-compare
pfultz2 b3af86c
Make test todo
pfultz2 62d7377
Merge branch 'main' into valueflow-infer-unsigned-compare
pfultz2 0377a41
Remove else
pfultz2 c8fcd31
Merge branch 'main' into valueflow-infer-unsigned-compare
pfultz2 0ccd789
Rerun dmake
pfultz2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix bug in removing contradictions
- Loading branch information
commit 75e8916aeff3010e0e568de8af3414a50a303381
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
#include <algorithm> | ||
#include <cassert> | ||
#include <cctype> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <cstring> | ||
#include <iostream> | ||
|
@@ -1946,6 +1947,17 @@ const Token *Token::getValueTokenDeadPointer() const | |
return nullptr; | ||
} | ||
|
||
static bool isAdjacent(const ValueFlow::Value& x, const ValueFlow::Value& y) | ||
{ | ||
if (x.bound == y.bound) { | ||
return true; | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This else is redundant |
||
if (x.valueType == ValueFlow::Value::ValueType::FLOAT) | ||
return false; | ||
return std::abs(x.intvalue - y.intvalue) == 1; | ||
} | ||
} | ||
|
||
static bool removeContradiction(std::list<ValueFlow::Value>& values) | ||
{ | ||
bool result = false; | ||
|
@@ -1963,70 +1975,37 @@ static bool removeContradiction(std::list<ValueFlow::Value>& values) | |
continue; | ||
if (!x.equalValue(y)) | ||
continue; | ||
if (x.bound == y.bound || | ||
(x.bound != ValueFlow::Value::Bound::Point && y.bound != ValueFlow::Value::Bound::Point)) { | ||
if (x.bound == y.bound) { | ||
const bool removex = !x.isImpossible() || y.isKnown(); | ||
const bool removey = !y.isImpossible() || x.isKnown(); | ||
if (removex) | ||
values.remove(x); | ||
if (removey) | ||
values.remove(y); | ||
return true; | ||
} else if (x.bound == ValueFlow::Value::Bound::Point) { | ||
y.decreaseRange(); | ||
result = true; | ||
} else { | ||
if (x.bound != ValueFlow::Value::Bound::Point) { | ||
// Only decrease the range on possible values | ||
if (!x.isImpossible()) | ||
x.decreaseRange(); | ||
} else { | ||
values.remove(x); | ||
return true; | ||
} | ||
if (y.bound != ValueFlow::Value::Bound::Point) { | ||
// Only decrease the range on possible values | ||
if (!y.isImpossible()) | ||
y.decreaseRange(); | ||
} else { | ||
values.remove(y); | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
static void removeOverlaps(std::list<ValueFlow::Value>& values) | ||
{ | ||
for (ValueFlow::Value& x : values) { | ||
if (x.isNonValue()) | ||
continue; | ||
values.remove_if([&](ValueFlow::Value& y) { | ||
if (y.isNonValue()) | ||
return false; | ||
if (&x == &y) | ||
return false; | ||
if (x.valueType != y.valueType) | ||
return false; | ||
if (x.valueKind != y.valueKind) | ||
return false; | ||
// TODO: Remove points coverd in a lower or upper bound | ||
// TODO: Remove lower or upper bound already covered by a lower and upper bound | ||
if (!x.equalValue(y)) | ||
return false; | ||
if (x.bound != y.bound) | ||
return false; | ||
return true; | ||
}); | ||
} | ||
} | ||
|
||
// Removing contradictions is an NP-hard problem. Instead we run multiple | ||
// passes to try to catch most contradictions | ||
static void removeContradictions(std::list<ValueFlow::Value>& values) | ||
{ | ||
for (int i = 0; i < 4; i++) { | ||
if (!removeContradiction(values)) | ||
return; | ||
removeOverlaps(values); | ||
} | ||
} | ||
|
||
static bool isAdjacent(const ValueFlow::Value& x, const ValueFlow::Value& y) | ||
{ | ||
if (x.bound == y.bound) { | ||
return true; | ||
} else { | ||
if (x.valueType == ValueFlow::Value::ValueType::FLOAT) | ||
return false; | ||
return std::abs(x.intvalue - y.intvalue) == 1; | ||
} | ||
} | ||
|
||
using ValueIterator = std::list<ValueFlow::Value>::iterator; | ||
|
||
|
@@ -2052,12 +2031,7 @@ static ValueIterator removeAdjacentValues(std::list<ValueFlow::Value>& values, V | |
|
||
static void mergeAdjacent(std::list<ValueFlow::Value>& values) | ||
{ | ||
auto prev = values.end(); | ||
for (auto x = values.begin(); x != values.end();) { | ||
if (x == prev) | ||
printf("Infinite\n"); | ||
// assert(x != prev); | ||
prev = x; | ||
if (x->isNonValue()) { | ||
x++; | ||
continue; | ||
|
@@ -2105,6 +2079,45 @@ static void mergeAdjacent(std::list<ValueFlow::Value>& values) | |
} | ||
} | ||
|
||
static void removeOverlaps(std::list<ValueFlow::Value>& values) | ||
{ | ||
for (ValueFlow::Value& x : values) { | ||
if (x.isNonValue()) | ||
continue; | ||
values.remove_if([&](ValueFlow::Value& y) { | ||
if (y.isNonValue()) | ||
return false; | ||
if (&x == &y) | ||
return false; | ||
if (x.valueType != y.valueType) | ||
return false; | ||
if (x.valueKind != y.valueKind) | ||
return false; | ||
// TODO: Remove points coverd in a lower or upper bound | ||
// TODO: Remove lower or upper bound already covered by a lower and upper bound | ||
if (!x.equalValue(y)) | ||
return false; | ||
if (x.bound != y.bound) | ||
return false; | ||
return true; | ||
}); | ||
} | ||
mergeAdjacent(values); | ||
} | ||
|
||
// Removing contradictions is an NP-hard problem. Instead we run multiple | ||
// passes to try to catch most contradictions | ||
static void removeContradictions(std::list<ValueFlow::Value>& values) | ||
{ | ||
removeOverlaps(values); | ||
for (int i = 0; i < 4; i++) { | ||
if (!removeContradiction(values)) | ||
return; | ||
removeOverlaps(values); | ||
} | ||
} | ||
|
||
|
||
bool Token::addValue(const ValueFlow::Value &value) | ||
{ | ||
if (value.isKnown() && mImpl->mValues) { | ||
|
@@ -2190,8 +2203,6 @@ bool Token::addValue(const ValueFlow::Value &value) | |
mImpl->mValues = new std::list<ValueFlow::Value>(1, v); | ||
} | ||
|
||
removeOverlaps(*mImpl->mValues); | ||
mergeAdjacent(*mImpl->mValues); | ||
removeContradictions(*mImpl->mValues); | ||
|
||
return true; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems my editor sometimes insert these headers. I'll remove them.