Skip to content

Commit bbf8dee

Browse files
committed
Bad overflow check broken into three steps
When demoing this, I found it more convenient to split the query into three steps so there are also some false positives to look at along the way.
1 parent 7986546 commit bbf8dee

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)