Skip to content

Commit db58704

Browse files
committed
Test large and negative constants, use BigInt
1 parent 6845cdc commit db58704

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

cpp/misra/src/rules/RULE-7-0-4/InappropriateBitwiseOrShiftOperands.ql

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,23 @@ predicate isValidShiftConstantRange(Expr right, Type leftType) {
3434
}
3535

3636
predicate isSignedConstantLeftShiftException(LShiftExpr shift) {
37-
exists(Expr left, Expr right, NumericType leftType, int leftVal, int rightVal, int maxBit |
37+
exists(
38+
Expr left, Expr right, NumericType leftType, QlBuiltins::BigInt leftVal, int rightVal,
39+
int maxBit
40+
|
3841
left = shift.getLeftOperand() and
3942
right = shift.getRightOperand() and
4043
leftType = left.getType() and
4144
isConstantExpression(left) and
4245
isConstantExpression(right) and
4346
isSignedType(leftType) and
4447
isValidShiftConstantRange(right, leftType) and
45-
leftVal = left.getValue().toInt() and
48+
leftVal = left.getValue().toBigInt() and
4649
rightVal = right.getValue().toInt() and
47-
leftVal >= 0 and
50+
leftVal >= 0.toBigInt() and
4851
maxBit = leftType.getSize() * 8 - 1 and
4952
// Check that no set bit is shifted into or beyond the sign bit
50-
leftVal * 2.pow(rightVal) < 2.pow(maxBit)
53+
leftVal * 2.toBigInt().pow(rightVal) < 2.toBigInt().pow(maxBit)
5154
)
5255
}
5356

@@ -111,7 +114,6 @@ where
111114
or
112115
// Shift operators - right operand must be unsigned or constant in valid range
113116
exists(BinaryShiftOperation shift, Expr right |
114-
shift = x and
115117
shift = x and
116118
right = shift.getRightOperand() and
117119
not isUnsignedType(right.getExplicitlyConverted().getType()) and

cpp/misra/test/rules/RULE-7-0-4/InappropriateBitwiseOrShiftOperands.expected

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
| test.cpp:85:3:85:11 | ... << ... | Shift operator '<<' requires unsigned right operand or constant in valid range. |
1616
| test.cpp:86:3:86:11 | ... << ... | Shift operator '<<' requires unsigned right operand or constant in valid range. |
1717
| test.cpp:87:3:87:11 | ... >> ... | Shift operator '>>' requires unsigned right operand or constant in valid range. |
18-
| test.cpp:97:3:97:9 | ... << ... | Shift operator '<<' requires unsigned left operand. |
19-
| test.cpp:98:3:98:9 | ... << ... | Shift operator '<<' requires unsigned left operand. |
20-
| test.cpp:99:3:99:9 | ... << ... | Shift operator '<<' requires unsigned left operand. |
21-
| test.cpp:100:3:100:9 | ... << ... | Shift operator '<<' requires unsigned left operand. |
22-
| test.cpp:104:3:104:11 | ... << ... | Shift operator '<<' requires unsigned left operand. |
23-
| test.cpp:106:3:106:17 | ... << ... | Shift operator '<<' requires unsigned left operand. |
24-
| test.cpp:112:3:112:10 | ... << ... | Shift operator '<<' requires unsigned left operand. |
25-
| test.cpp:120:3:120:11 | ... >> ... | Shift operator '>>' requires unsigned left operand. |
18+
| test.cpp:93:3:93:11 | ... << ... | Shift operator '<<' requires unsigned right operand or constant in valid range. |
19+
| test.cpp:94:3:94:11 | ... << ... | Shift operator '<<' requires unsigned right operand or constant in valid range. |
20+
| test.cpp:95:3:95:11 | ... >> ... | Shift operator '>>' requires unsigned right operand or constant in valid range. |
21+
| test.cpp:96:3:96:11 | ... >> ... | Shift operator '>>' requires unsigned right operand or constant in valid range. |
22+
| test.cpp:106:3:106:9 | ... << ... | Shift operator '<<' requires unsigned left operand. |
23+
| test.cpp:107:3:107:9 | ... << ... | Shift operator '<<' requires unsigned left operand. |
24+
| test.cpp:108:3:108:9 | ... << ... | Shift operator '<<' requires unsigned left operand. |
25+
| test.cpp:109:3:109:9 | ... << ... | Shift operator '<<' requires unsigned left operand. |
26+
| test.cpp:113:3:113:11 | ... << ... | Shift operator '<<' requires unsigned left operand. |
27+
| test.cpp:117:3:117:30 | ... << ... | Shift operator '<<' requires unsigned left operand. |
28+
| test.cpp:119:3:119:17 | ... << ... | Shift operator '<<' requires unsigned left operand. |
29+
| test.cpp:125:3:125:10 | ... << ... | Shift operator '<<' requires unsigned left operand. |
30+
| test.cpp:133:3:133:11 | ... >> ... | Shift operator '>>' requires unsigned left operand. |

cpp/misra/test/rules/RULE-7-0-4/test.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,16 @@ void test_shift_operators_right_operand_constant_range() {
8787
u32 >> 32; // NON_COMPLIANT
8888
}
8989

90-
void test_exception_signed_constant_left_operand() {
90+
void test_shift_operators_negative_right_operand() {
91+
std::uint32_t u32 = 0x12345678U;
92+
93+
u32 << -1; // NON_COMPLIANT
94+
u32 << -5; // NON_COMPLIANT
95+
u32 >> -1; // NON_COMPLIANT
96+
u32 >> -3; // NON_COMPLIANT
97+
}
98+
99+
void test_exception_signed_constant_left_operand_exception() {
91100
// Exception cases for signed constant expressions
92101
1 << 30; // COMPLIANT
93102
2 << 29; // COMPLIANT
@@ -99,9 +108,13 @@ void test_exception_signed_constant_left_operand() {
99108
4 << 29; // NON_COMPLIANT
100109
8 << 28; // NON_COMPLIANT
101110

102-
1LL << 31; // COMPLIANT - 64 bit type
103-
1LL << 62; // COMPLIANT - 64 bit type
104-
1LL << 63; // NON_COMPLIANT - 64 bit type
111+
1LL << 31; // COMPLIANT - 64 bit type
112+
1LL << 62; // COMPLIANT - 64 bit type
113+
1LL << 63; // NON_COMPLIANT - 64 bit type
114+
0x1000'0000'0000'0000LL << 2; // COMPLIANT
115+
0x2000'0000'0000'0000LL << 1; // COMPLIANT
116+
117+
0x4000'0000'0000'0000LL << 1; // NON_COMPLIANT
105118

106119
0x40000000 << 1; // NON_COMPLIANT
107120
}

0 commit comments

Comments
 (0)