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

C++ SimpleRangeAnalysis: remove float bounds #16862

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,6 @@ private predicate typeBounds(ArithmeticType t, float lb, float ub) {
lb = 0 and ub = limit - 1
)
)
or
// This covers all floating point types. The range is (-Inf, +Inf).
t instanceof FloatingPointType and lb = -(1.0 / 0.0) and ub = 1.0 / 0.0
}

private Type stripReference(Type t) {
Expand Down
2 changes: 2 additions & 0 deletions cpp/ql/src/Security/CWE/CWE-190/ArithmeticUncontrolled.ql
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ module UncontrolledArithConfig implements DataFlow::ConfigSig {
op instanceof ComplementExpr
).getAnOperand*()
or
node.asExpr().getUnderlyingType() instanceof FloatingPointType
or
// block unintended flow to pointers
node.asExpr().getUnspecifiedType() instanceof PointerType
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ int overeager_wraparound(unsigned int u32bound, unsigned long long u64bound) {

int negative_zero(double dbl) {
if (dbl >= 0) {
return dbl >= -dbl; // GOOD [FALSE POSITIVE]
return dbl >= -dbl; // GOOD
}
return 0;
}
Expand Down Expand Up @@ -334,7 +334,7 @@ int nan2(double x) {
if (x < 0.0) {
return 100;
}
else if (x >= 0.0) { // BAD [Always true]
else if (x >= 0.0) { // BAD [Always true] [NOT DETECTED]
return 200;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@
| PointlessComparison.c:126:12:126:18 | ... >= ... | Comparison is always true because a >= 20. |
| PointlessComparison.c:129:12:129:16 | ... > ... | Comparison is always false because a <= 3. |
| PointlessComparison.c:197:7:197:11 | ... < ... | Comparison is always false because x >= 0. |
| PointlessComparison.c:264:12:264:22 | ... >= ... | Comparison is always true because dbl >= 0 and 0 >= - .... |
| PointlessComparison.c:273:9:273:18 | ... > ... | Comparison is always false because c <= 0. |
| PointlessComparison.c:283:13:283:19 | ... >= ... | Comparison is always true because c >= 11. |
| PointlessComparison.c:294:9:294:16 | ... >= ... | Comparison is always false because ui1 <= 0. |
| PointlessComparison.c:303:9:303:14 | ... >= ... | Comparison is always false because c <= 0. |
| PointlessComparison.c:312:9:312:14 | ... >= ... | Comparison is always false because c <= 0. |
| PointlessComparison.c:337:14:337:21 | ... >= ... | Comparison is always true because x >= 0. |
| PointlessComparison.c:372:6:372:16 | ... >= ... | Comparison is always true because ... >> ... >= 1. |
| PointlessComparison.c:373:6:373:16 | ... >= ... | Comparison is always false because ... >> ... <= 1. |
| PointlessComparison.c:383:6:383:17 | ... >= ... | Comparison is always false because ... & ... <= 2. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ edges
| test.cpp:151:10:151:13 | call to rand | test.cpp:151:10:151:13 | call to rand | provenance | |
| test.cpp:151:10:151:13 | call to rand | test.cpp:153:10:153:15 | ... - ... | provenance | |
| test.cpp:153:10:153:15 | ... - ... | test.cpp:154:10:154:10 | b | provenance | |
| test.cpp:169:11:169:14 | call to rand | test.cpp:169:11:169:14 | call to rand | provenance | |
| test.cpp:169:11:169:14 | call to rand | test.cpp:170:13:170:13 | x | provenance | |
| test.cpp:170:13:170:13 | x | test.cpp:171:11:171:16 | y | provenance | |
| test.cpp:189:10:189:13 | call to rand | test.cpp:189:10:189:13 | call to rand | provenance | |
| test.cpp:189:10:189:13 | call to rand | test.cpp:195:3:195:11 | ... = ... | provenance | |
| test.cpp:189:10:189:13 | call to rand | test.cpp:198:3:198:11 | ... = ... | provenance | |
Expand Down Expand Up @@ -119,10 +116,6 @@ nodes
| test.cpp:151:10:151:13 | call to rand | semmle.label | call to rand |
| test.cpp:153:10:153:15 | ... - ... | semmle.label | ... - ... |
| test.cpp:154:10:154:10 | b | semmle.label | b |
| test.cpp:169:11:169:14 | call to rand | semmle.label | call to rand |
| test.cpp:169:11:169:14 | call to rand | semmle.label | call to rand |
| test.cpp:170:13:170:13 | x | semmle.label | x |
| test.cpp:171:11:171:16 | y | semmle.label | y |
| test.cpp:189:10:189:13 | call to rand | semmle.label | call to rand |
| test.cpp:189:10:189:13 | call to rand | semmle.label | call to rand |
| test.cpp:190:10:190:13 | call to rand | semmle.label | call to rand |
Expand Down Expand Up @@ -160,7 +153,6 @@ subpaths
| test.cpp:102:10:102:10 | x | test.cpp:98:10:98:13 | call to rand | test.cpp:102:10:102:10 | x | This arithmetic expression depends on an $@, potentially causing an overflow. | test.cpp:98:10:98:13 | call to rand | uncontrolled value |
| test.cpp:146:9:146:9 | y | test.cpp:137:10:137:13 | call to rand | test.cpp:146:9:146:9 | y | This arithmetic expression depends on an $@, potentially causing an overflow. | test.cpp:137:10:137:13 | call to rand | uncontrolled value |
| test.cpp:154:10:154:10 | b | test.cpp:151:10:151:13 | call to rand | test.cpp:154:10:154:10 | b | This arithmetic expression depends on an $@, potentially causing an overflow. | test.cpp:151:10:151:13 | call to rand | uncontrolled value |
| test.cpp:171:11:171:16 | y | test.cpp:169:11:169:14 | call to rand | test.cpp:171:11:171:16 | y | This arithmetic expression depends on an $@, potentially causing an overflow. | test.cpp:169:11:169:14 | call to rand | uncontrolled value |
| test.cpp:196:7:196:7 | x | test.cpp:189:10:189:13 | call to rand | test.cpp:196:7:196:7 | x | This arithmetic expression depends on an $@, potentially causing an overflow. | test.cpp:189:10:189:13 | call to rand | uncontrolled value |
| test.cpp:198:7:198:7 | x | test.cpp:189:10:189:13 | call to rand | test.cpp:198:7:198:7 | x | This arithmetic expression depends on an $@, potentially causing an overflow. | test.cpp:189:10:189:13 | call to rand | uncontrolled value |
| test.cpp:199:7:199:7 | x | test.cpp:189:10:189:13 | call to rand | test.cpp:199:7:199:7 | x | This arithmetic expression depends on an $@, potentially causing an overflow. | test.cpp:189:10:189:13 | call to rand | uncontrolled value |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ void randomTester() {
int i;
for (i = 0; i < 1000; i++) {
int r = rand();
// BAD: The return from rand() is unbounded

// BAD: The return from rand() is unbounded
trySlice(r, r+100);
}

Expand All @@ -44,24 +44,24 @@ void randomTester() {
int r = rand();
r += 100; // BAD
}

{
int r = rand() / 10;
r += 100; // GOOD
}

{
int r = rand();
r = r / 10;
r += 100; // GOOD
}

{
int r = rand();
r /= 10;
r += 100; // GOOD
}

{
int r = rand() & 0xFF;
r += 100; // GOOD
Expand Down Expand Up @@ -123,13 +123,13 @@ void randomTester2(int bound, int min, int max) {
void moreTests() {
{
int r = rand();

r = r * 100; // BAD
}

{
int r = rand();

r *= 100; // BAD
}

Expand All @@ -141,19 +141,19 @@ void moreTests() {

{
int r = rand();

r <<= 8; // BAD [NOT DETECTED]
}

{
int r = rand();

r = r - 100; // GOOD
}

{
unsigned int r = rand();

r = r - 100; // BAD
}
}
Expand All @@ -164,4 +164,4 @@ void guarded_test(unsigned p) {
return;
}
unsigned z = data - p; // GOOD
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int test_conditional_assignment_2()
{
y = x;
}

return y * 10; // GOOD (as y <= 100)
}

Expand All @@ -142,7 +142,7 @@ int test_conditional_assignment_3()
{
y = x;
}

return y * c; // GOOD (as y <= 100) [FALSE POSITIVE]
}

Expand All @@ -168,7 +168,7 @@ void test_float()
{
int x = rand();
float y = x; // GOOD
int z = (int)y * 5; // BAD
int z = (int)y * 5; // BAD [NOT DETECTED]
}

{
Expand Down
Loading