Skip to content

Commit d7137ec

Browse files
committed
Add compliance tests for division and modulus operations in unsigned operations
1 parent 883a46f commit d7137ec

4 files changed

Lines changed: 19 additions & 2 deletions

File tree

c/common/test/rules/unsignedoperationwithconstantoperandswraps/test.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,11 @@ void test_sub_postcheck(unsigned int i1, unsigned int i2) {
8080
if (i1 > i2) {
8181
// handle error
8282
}
83+
84+
void test_mod_rem(unsigned int i1, unsigned int i2) {
85+
i1 / i2; // COMPLIANT - exception 2
86+
i1 /= i2; // COMPLIANT - exception 2
87+
i1 % i2; // COMPLIANT - exception 2
88+
i1 %= i2; // COMPLIANT - exception 2
89+
}
8390
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- `INT30-C` - `UnsignedIntegerOperationsWrapAround.ql`:
2+
- Fixed false positives for `/=` and `%=` assignments.
3+
- `INT30-C` - `UnsignedOperationWithConstantOperandsWraps.ql`:
4+
- Fixed false positives for `/=` and `%=` assignments.

cpp/common/src/codingstandards/cpp/rules/unsignedoperationwithconstantoperandswraps/UnsignedOperationWithConstantOperandsWraps.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ query predicate problems(InterestingOverflowingOperation op, string message) {
2626
// Permitted by exception 3
2727
not op instanceof LShiftExpr and
2828
// Permitted by exception 2 - zero case is handled in separate query
29-
not op instanceof DivExpr and
30-
not op instanceof RemExpr and
29+
not op instanceof DivOrRemOperation and
3130
message =
3231
"Operation " + op.getOperator() + " of type " + op.getType().getUnderlyingType() + " may wrap."
3332
}

cpp/common/test/rules/unsignedoperationwithconstantoperandswraps/test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,11 @@ void test_sub_postcheck(unsigned int i1, unsigned int i2) {
8080
if (i1 > i2) {
8181
// handle error
8282
}
83+
84+
void test_mod_rem(unsigned int i1, unsigned int i2) {
85+
i1 / i2; // COMPLIANT - exception 2
86+
i1 /= i2; // COMPLIANT - exception 2
87+
i1 % i2; // COMPLIANT - exception 2
88+
i1 %= i2; // COMPLIANT - exception 2
89+
}
8390
}

0 commit comments

Comments
 (0)