Skip to content

Commit 3b437fe

Browse files
committed
C++: Replace GVN with some other libraries.
1 parent 517fd23 commit 3b437fe

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

cpp/ql/src/Security/CWE/CWE-191/UnsignedDifferenceExpressionComparedZero.ql

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
import cpp
1414
import semmle.code.cpp.commons.Exclusions
15-
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
1615
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
1716
import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
1817
import semmle.code.cpp.controlflow.Guards
18+
import semmle.code.cpp.dataflow.DataFlow
1919

2020
/**
2121
* Holds if `sub` is guarded by a condition which ensures that
@@ -37,15 +37,27 @@ predicate exprIsSubLeftOrLess(SubExpr sub, Expr e) {
3737
e = sub.getLeftOperand()
3838
or
3939
exists(Expr other |
40-
// GVN equality
40+
// use-use
4141
exprIsSubLeftOrLess(sub, other) and
42-
globalValueNumber(e) = globalValueNumber(other)
42+
(
43+
useUsePair(_, other, e) or
44+
useUsePair(_, e, other)
45+
)
46+
)
47+
or
48+
exists(Expr other |
49+
// dataflow
50+
exprIsSubLeftOrLess(sub, other) and
51+
(
52+
DataFlow::localFlowStep(DataFlow::exprNode(e), DataFlow::exprNode(other)) or
53+
DataFlow::localFlowStep(DataFlow::exprNode(other), DataFlow::exprNode(e))
54+
)
4355
)
4456
or
4557
exists(Expr other |
4658
// guard constraining `sub`
4759
exprIsSubLeftOrLess(sub, other) and
48-
isGuarded(sub, other, e) // left >= right
60+
isGuarded(sub, other, e) // other >= e
4961
)
5062
or
5163
exists(Expr other, float p, float q |

cpp/ql/test/query-tests/Security/CWE/CWE-191/UnsignedDifferenceExpressionComparedZero/UnsignedDifferenceExpressionComparedZero.expected

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
| test.cpp:6:5:6:13 | ... > ... | Unsigned subtraction can never be negative. |
22
| test.cpp:10:8:10:24 | ... > ... | Unsigned subtraction can never be negative. |
3-
| test.cpp:15:9:15:25 | ... > ... | Unsigned subtraction can never be negative. |
4-
| test.cpp:32:12:32:20 | ... > ... | Unsigned subtraction can never be negative. |
5-
| test.cpp:39:12:39:20 | ... > ... | Unsigned subtraction can never be negative. |
6-
| test.cpp:47:5:47:13 | ... > ... | Unsigned subtraction can never be negative. |
7-
| test.cpp:55:5:55:13 | ... > ... | Unsigned subtraction can never be negative. |
83
| test.cpp:62:5:62:13 | ... > ... | Unsigned subtraction can never be negative. |
9-
| test.cpp:69:5:69:13 | ... > ... | Unsigned subtraction can never be negative. |
104
| test.cpp:75:8:75:16 | ... > ... | Unsigned subtraction can never be negative. |
115
| test.cpp:101:6:101:14 | ... > ... | Unsigned subtraction can never be negative. |
126
| test.cpp:128:6:128:14 | ... > ... | Unsigned subtraction can never be negative. |
137
| test.cpp:137:6:137:14 | ... > ... | Unsigned subtraction can never be negative. |
148
| test.cpp:146:7:146:15 | ... > ... | Unsigned subtraction can never be negative. |
159
| test.cpp:152:7:152:15 | ... > ... | Unsigned subtraction can never be negative. |
1610
| test.cpp:182:6:182:14 | ... > ... | Unsigned subtraction can never be negative. |
17-
| test.cpp:195:6:195:14 | ... > ... | Unsigned subtraction can never be negative. |
1811
| test.cpp:208:6:208:14 | ... > ... | Unsigned subtraction can never be negative. |
1912
| test.cpp:252:10:252:18 | ... > ... | Unsigned subtraction can never be negative. |
2013
| test.cpp:266:10:266:24 | ... > ... | Unsigned subtraction can never be negative. |

cpp/ql/test/query-tests/Security/CWE/CWE-191/UnsignedDifferenceExpressionComparedZero/test.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void test(unsigned x, unsigned y, bool unknown) {
1212
}
1313

1414
if(total <= limit) {
15-
while(limit - total > 0) { // GOOD [FALSE POSITIVE]
15+
while(limit - total > 0) { // GOOD
1616
total += getAnInt();
1717
if(total > limit) break;
1818
}
@@ -29,30 +29,30 @@ void test(unsigned x, unsigned y, bool unknown) {
2929
} else {
3030
y = x;
3131
}
32-
bool b1 = x - y > 0; // GOOD [FALSE POSITIVE]
32+
bool b1 = x - y > 0; // GOOD
3333

3434
x = getAnInt();
3535
y = getAnInt();
3636
if(y > x) {
3737
y = x - 1;
3838
}
39-
bool b2 = x - y > 0; // GOOD [FALSE POSITIVE]
39+
bool b2 = x - y > 0; // GOOD
4040

4141
int N = getAnInt();
4242
y = x;
4343
while(cond()) {
4444
if(unknown) { y--; }
4545
}
4646

47-
if(x - y > 0) { } // GOOD [FALSE POSITIVE]
47+
if(x - y > 0) { } // GOOD
4848

4949
x = y;
5050
while(cond()) {
5151
if(unknown) break;
5252
y--;
5353
}
5454

55-
if(x - y > 0) { } // GOOD [FALSE POSITIVE]
55+
if(x - y > 0) { } // GOOD
5656

5757
y = 0;
5858
for(int i = 0; i < x; ++i) {
@@ -66,7 +66,7 @@ void test(unsigned x, unsigned y, bool unknown) {
6666
if(unknown) { x++; }
6767
}
6868

69-
if(x - y > 0) { } // GOOD [FALSE POSITIVE]
69+
if(x - y > 0) { } // GOOD
7070

7171
int n = getAnInt();
7272
if (n > x - y) { n = x - y; }
@@ -192,7 +192,7 @@ void test10() {
192192
a = b;
193193
}
194194

195-
if (a - b > 0) { // GOOD (as a >= b) [FALSE POSITIVE]
195+
if (a - b > 0) { // GOOD (as a >= b)
196196
// ...
197197
}
198198
}

0 commit comments

Comments
 (0)