Skip to content

Commit 0e7eeb3

Browse files
authored
Merge pull request github#5678 from MathiasVP/sound-expr-might-overflow-predicate
C++: Make exprMightOverflowPositively sound for unanalyzable expressions
2 parents a09c12a + 772d5ea commit 0e7eeb3

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* The `exprMightOverflowPositively` and `exprMightOverflowNegatively` predicates from the `SimpleRangeAnalysis` library now recognize more expressions that might overflow.

cpp/ql/src/Security/CWE/CWE-190/IntegerOverflowTainted.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ predicate outOfBoundsExpr(Expr expr, string kind) {
2828

2929
from Expr use, Expr origin, string kind
3030
where
31+
not use.getUnspecifiedType() instanceof PointerType and
3132
outOfBoundsExpr(use, kind) and
3233
tainted(origin, use) and
3334
origin != use and

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,18 @@ private module SimpleRangeAnalysisCached {
16171617
defMightOverflowPositively(def, v)
16181618
}
16191619

1620+
/**
1621+
* Holds if `e` is an expression where the concept of overflow makes sense.
1622+
* This predicate is used to filter out some of the unanalyzable expressions
1623+
* from `exprMightOverflowPositively` and `exprMightOverflowNegatively`.
1624+
*/
1625+
pragma[inline]
1626+
private predicate exprThatCanOverflow(Expr e) {
1627+
e instanceof UnaryArithmeticOperation or
1628+
e instanceof BinaryArithmeticOperation or
1629+
e instanceof LShiftExpr
1630+
}
1631+
16201632
/**
16211633
* Holds if the expression might overflow negatively. This predicate
16221634
* does not consider the possibility that the expression might overflow
@@ -1630,6 +1642,11 @@ private module SimpleRangeAnalysisCached {
16301642
// bound of `x`, so the standard logic (above) does not work for
16311643
// detecting whether it might overflow.
16321644
getLowerBoundsImpl(expr.(PostfixDecrExpr)) = exprMinVal(expr)
1645+
or
1646+
// We can't conclude that any unanalyzable expression might overflow. This
1647+
// is because there are many expressions that the range analysis doesn't
1648+
// handle, but where the concept of overflow doesn't make sense.
1649+
exprThatCanOverflow(expr) and not analyzableExpr(expr)
16331650
}
16341651

16351652
/**
@@ -1657,6 +1674,11 @@ private module SimpleRangeAnalysisCached {
16571674
// bound of `x`, so the standard logic (above) does not work for
16581675
// detecting whether it might overflow.
16591676
getUpperBoundsImpl(expr.(PostfixIncrExpr)) = exprMaxVal(expr)
1677+
or
1678+
// We can't conclude that any unanalyzable expression might overflow. This
1679+
// is because there are many expressions that the range analysis doesn't
1680+
// handle, but where the concept of overflow doesn't make sense.
1681+
exprThatCanOverflow(expr) and not analyzableExpr(expr)
16601682
}
16611683

16621684
/**

0 commit comments

Comments
 (0)