Skip to content

Commit afa8925

Browse files
authored
Merge pull request github#5780 from MathiasVP/cleanup-missingGuard-predicates-after-range-analysis-fix
C++: Cleanup missingGuardAgainstOverflow
2 parents 64a2320 + 04a785b commit afa8925

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

cpp/ql/src/semmle/code/cpp/rangeanalysis/SimpleRangeAnalysis.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,9 @@ private module SimpleRangeAnalysisCached {
16261626
private predicate exprThatCanOverflow(Expr e) {
16271627
e instanceof UnaryArithmeticOperation or
16281628
e instanceof BinaryArithmeticOperation or
1629-
e instanceof LShiftExpr
1629+
e instanceof AssignArithmeticOperation or
1630+
e instanceof LShiftExpr or
1631+
e instanceof AssignLShiftExpr
16301632
}
16311633

16321634
/**

cpp/ql/src/semmle/code/cpp/security/Overflow.qll

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ VariableAccess varUse(LocalScopeVariable v) { result = v.getAnAccess() }
9999
* Holds if `e` potentially overflows and `use` is an operand of `e` that is not guarded.
100100
*/
101101
predicate missingGuardAgainstOverflow(Operation e, VariableAccess use) {
102-
(
103-
convertedExprMightOverflowPositively(e)
104-
or
105-
// Ensure that the predicate holds when range analysis cannot determine an upper bound
106-
upperBound(e.getFullyConverted()) = exprMaxVal(e.getFullyConverted())
107-
) and
102+
// Since `e` is guarenteed to be a `BinaryArithmeticOperation`, a `UnaryArithmeticOperation` or
103+
// an `AssignArithmeticOperation` by the other constraints in this predicate, we know that
104+
// `convertedExprMightOverflowPositively` will have a result even when `e` is not analyzable
105+
// by `SimpleRangeAnalysis`.
106+
convertedExprMightOverflowPositively(e) and
108107
use = e.getAnOperand() and
109108
exists(LocalScopeVariable v | use.getTarget() = v |
110109
// overflow possible if large
@@ -126,12 +125,11 @@ predicate missingGuardAgainstOverflow(Operation e, VariableAccess use) {
126125
* Holds if `e` potentially underflows and `use` is an operand of `e` that is not guarded.
127126
*/
128127
predicate missingGuardAgainstUnderflow(Operation e, VariableAccess use) {
129-
(
130-
convertedExprMightOverflowNegatively(e)
131-
or
132-
// Ensure that the predicate holds when range analysis cannot determine a lower bound
133-
lowerBound(e.getFullyConverted()) = exprMinVal(e.getFullyConverted())
134-
) and
128+
// Since `e` is guarenteed to be a `BinaryArithmeticOperation`, a `UnaryArithmeticOperation` or
129+
// an `AssignArithmeticOperation` by the other constraints in this predicate, we know that
130+
// `convertedExprMightOverflowNegatively` will have a result even when `e` is not analyzable
131+
// by `SimpleRangeAnalysis`.
132+
convertedExprMightOverflowNegatively(e) and
135133
use = e.getAnOperand() and
136134
exists(LocalScopeVariable v | use.getTarget() = v |
137135
// underflow possible if use is left operand and small

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/extreme/ArithmeticWithExtremeValues.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
| test.c:50:3:50:5 | sc3 | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:49:9:49:16 | 127 | Extreme value |
44
| test.c:59:3:59:5 | sc6 | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:58:9:58:16 | 127 | Extreme value |
55
| test.c:63:3:63:5 | sc8 | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:62:9:62:16 | - ... | Extreme value |
6-
| test.c:75:3:75:5 | sc1 | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:74:9:74:16 | 127 | Extreme value |
76
| test.c:124:9:124:9 | x | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:118:17:118:23 | 2147483647 | Extreme value |

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/extreme/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void test_negatives() {
7272
signed char sc1, sc2, sc3, sc4, sc5, sc6, sc7, sc8;
7373

7474
sc1 = CHAR_MAX;
75-
sc1 += 0; // GOOD [FALSE POSITIVE]
75+
sc1 += 0; // GOOD
7676
sc1 += -1; // GOOD
7777
sc2 = CHAR_MIN;
7878
sc2 += -1; // BAD [NOT DETECTED]

0 commit comments

Comments
 (0)