Skip to content

Commit 2411d50

Browse files
Merge pull request #8 from Semmle/bad-overflow-steps
Bad overflow check broken into three steps
2 parents 7986546 + e530f11 commit 2411d50

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import cpp
2+
3+
/** Matches `var < var + ???`. */
4+
predicate overflowCheck(LocalScopeVariable var, AddExpr add, RelationalOperation compare) {
5+
compare.getAnOperand() = var.getAnAccess() and
6+
compare.getAnOperand() = add and
7+
add.getAnOperand() = var.getAnAccess()
8+
}
9+
10+
from LocalScopeVariable var, AddExpr add
11+
where overflowCheck(var, add, _)
12+
select add, "Overflow check on variable of type " + var.getUnderlyingType()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import cpp
2+
3+
/** Matches `var < var + ???`. */
4+
predicate overflowCheck(LocalScopeVariable var, AddExpr add, RelationalOperation compare) {
5+
compare.getAnOperand() = var.getAnAccess() and
6+
compare.getAnOperand() = add and
7+
add.getAnOperand() = var.getAnAccess()
8+
}
9+
10+
from LocalScopeVariable var, AddExpr add
11+
where overflowCheck(var, add, _)
12+
and var.getType().getSize() < 4
13+
select add, "Overflow check on variable of type " + var.getUnderlyingType()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import cpp
2+
3+
/** Matches `var < var + ???`. */
4+
predicate overflowCheck(LocalScopeVariable var, AddExpr add, RelationalOperation compare) {
5+
compare.getAnOperand() = var.getAnAccess() and
6+
compare.getAnOperand() = add and
7+
add.getAnOperand() = var.getAnAccess()
8+
}
9+
10+
from LocalScopeVariable var, AddExpr add
11+
where overflowCheck(var, add, _)
12+
and var.getType().getSize() < 4
13+
and not add.getConversion+().getType().getSize() < 4
14+
select add, "Bad overflow check on variable of type " + var.getUnderlyingType()

0 commit comments

Comments
 (0)