Skip to content

Commit 60e4fab

Browse files
committed
C++: Add linear expression logic.
1 parent 48ff8e2 commit 60e4fab

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import cpp
1414
import semmle.code.cpp.commons.Exclusions
1515
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
1616
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
17+
import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
1718
import semmle.code.cpp.controlflow.Guards
1819

1920
/**
@@ -46,6 +47,22 @@ predicate exprIsSubLeftOrLess(SubExpr sub, Expr e) {
4647
exprIsSubLeftOrLess(sub, other) and
4748
isGuarded(sub, other, e) // left >= right
4849
)
50+
or
51+
exists(Expr other, float p, float q |
52+
// linear access of `other`
53+
exprIsSubLeftOrLess(sub, other) and
54+
linearAccess(e, other, p, q) and // e = p * other + q
55+
p <= 1 and
56+
q <= 0
57+
)
58+
or
59+
exists(Expr other, float p, float q |
60+
// linear access of `e`
61+
exprIsSubLeftOrLess(sub, other) and
62+
linearAccess(other, e, p, q) and // other = p * e + q
63+
p >= 1 and
64+
q >= 0
65+
)
4966
}
5067

5168
from RelationalOperation ro, SubExpr sub

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
| test.cpp:62:5:62:13 | ... > ... | Unsigned subtraction can never be negative. |
99
| test.cpp:69:5:69:13 | ... > ... | Unsigned subtraction can never be negative. |
1010
| test.cpp:75:8:75:16 | ... > ... | Unsigned subtraction can never be negative. |
11-
| test.cpp:92:6:92:14 | ... > ... | Unsigned subtraction can never be negative. |
1211
| test.cpp:101:6:101:14 | ... > ... | Unsigned subtraction can never be negative. |
13-
| test.cpp:119:6:119:14 | ... > ... | Unsigned subtraction can never be negative. |
1412
| test.cpp:128:6:128:14 | ... > ... | Unsigned subtraction can never be negative. |
1513
| test.cpp:137:6:137:14 | ... > ... | Unsigned subtraction can never be negative. |
1614
| test.cpp:146:7:146:15 | ... > ... | Unsigned subtraction can never be negative. |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void test3() {
8989
unsigned int a = getAnInt();
9090
unsigned int b = a - 1;
9191

92-
if (a - b > 0) { // GOOD (as a >= b) [FALSE POSITIVE]
92+
if (a - b > 0) { // GOOD (as a >= b)
9393
// ...
9494
}
9595
}
@@ -116,7 +116,7 @@ void test6() {
116116
unsigned int b = getAnInt();
117117
unsigned int a = b + 1;
118118

119-
if (a - b > 0) { // GOOD (as a >= b) [FALSE POSITIVE]
119+
if (a - b > 0) { // GOOD (as a >= b)
120120
// ...
121121
}
122122
}

0 commit comments

Comments
 (0)