Skip to content

Commit 032ac50

Browse files
committed
C++: Do not warn on static buffer overflow using loop counters, if the loop counter has been widened
1 parent 3e5f7d0 commit 032ac50

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,14 @@ private module SimpleRangeAnalysisCached {
15931593
result = min([max(getTruncatedUpperBounds(expr)), getGuardedUpperBound(expr)])
15941594
}
15951595

1596+
/** Holds if `expr` may have been widened */
1597+
cached
1598+
predicate upperBoundMayBeWidened(Expr e) {
1599+
isRecursiveExpr(e) and
1600+
// Corresponds to taking max on the RHS
1601+
not getGuardedUpperBound(e) < getTruncatedUpperBounds(e)
1602+
}
1603+
15961604
/**
15971605
* Holds if `expr` has a provably empty range. For example:
15981606
*

cpp/ql/src/Critical/OverflowStatic.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ predicate overflowOffsetInLoop(BufferAccess bufaccess, string msg) {
5555
loop.counter().getAnAccess() = bufaccess.getArrayOffset() and
5656
// Ensure that we don't have an upper bound on the array index that's less than the buffer size.
5757
not upperBound(bufaccess.getArrayOffset().getFullyConverted()) < bufaccess.bufferSize() and
58+
// The upper bounds analysis must not have been widended
59+
not upperBoundMayBeWidened(bufaccess.getArrayOffset().getFullyConverted()) and
5860
msg =
5961
"Potential buffer-overflow: counter '" + loop.counter().toString() + "' <= " +
6062
loop.limit().toString() + " but '" + bufaccess.buffer().getName() + "' has " +

0 commit comments

Comments
 (0)