Skip to content

Commit 3f2db7a

Browse files
committed
IntegerOverflow: clarify valid post-check
Only applicable to unsigned operations
1 parent a845f38 commit 3f2db7a

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

c/cert/src/rules/INT30-C/UnsignedIntegerOperationsWrapAround.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ where
2929
// Not guarded by a check, where the check is not an invalid overflow check
3030
not bop.getAGuardingGVN() = globalValueNumber(bop.getAChild*()) and
3131
// Is not checked after the operation
32-
not bop.hasPostCheck()
32+
not bop.hasValidPostCheck()
3333
select bop,
3434
"Binary expression ..." + bop.getOperator() + "... of type " + bop.getType().getUnderlyingType() +
3535
" may wrap."

cpp/common/src/codingstandards/cpp/Overflow.qll

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,27 @@ class InterestingBinaryOverflowingExpr extends BinaryArithmeticOperation {
4242
)
4343
}
4444

45-
predicate hasPostCheck() {
46-
exists(RelationalOperation ro |
47-
DataFlow::localExprFlow(this, ro.getLesserOperand()) and
48-
globalValueNumber(ro.getGreaterOperand()) = globalValueNumber(this.getAnOperand()) and
49-
this instanceof AddExpr and
50-
ro instanceof GuardCondition
51-
)
52-
or
53-
exists(RelationalOperation ro |
54-
DataFlow::localExprFlow(this, ro.getGreaterOperand()) and
55-
globalValueNumber(ro.getLesserOperand()) = globalValueNumber(this.getAnOperand()) and
56-
this instanceof SubExpr and
57-
ro instanceof GuardCondition
45+
/**
46+
* Holds if there is a correct validity check after this expression which may overflow.
47+
*
48+
* Only holds for unsigned expressions, as signed overflow/underflow are undefined behavior.
49+
*/
50+
predicate hasValidPostCheck() {
51+
this.getType().(IntegralType).isUnsigned() and
52+
(
53+
exists(RelationalOperation ro |
54+
DataFlow::localExprFlow(this, ro.getLesserOperand()) and
55+
globalValueNumber(ro.getGreaterOperand()) = globalValueNumber(this.getAnOperand()) and
56+
this instanceof AddExpr and
57+
ro instanceof GuardCondition
58+
)
59+
or
60+
exists(RelationalOperation ro |
61+
DataFlow::localExprFlow(this, ro.getGreaterOperand()) and
62+
globalValueNumber(ro.getLesserOperand()) = globalValueNumber(this.getAnOperand()) and
63+
this instanceof SubExpr and
64+
ro instanceof GuardCondition
65+
)
5866
)
5967
}
6068

0 commit comments

Comments
 (0)