Skip to content

Commit 5843258

Browse files
committed
Rule 7.0.6: Improve bitfield support
- Add extra testing - Support signed bitfields
1 parent ea7d168 commit 5843258

File tree

3 files changed

+246
-106
lines changed

3 files changed

+246
-106
lines changed

cpp/misra/src/rules/RULE-7-0-6/NumericAssignmentTypeMismatch.ql

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ predicate isValidConstantAssignment(Expr source, NumericType targetType) {
2727
exists(BitField bf, int numBits |
2828
isAssignedToBitfield(source, bf) and
2929
numBits = bf.getNumBits() and
30-
val >= 0 and
31-
val < 2.pow(numBits)
30+
if targetType.getSignedness() = Signed()
31+
then
32+
// Signed bit field: value must be in the range of signed bit field
33+
val >= -2.pow(numBits - 1) and
34+
val < 2.pow(numBits - 1)
35+
else (
36+
// Unsigned bit field: value must be in the range of unsigned bit field
37+
val >= 0 and
38+
val < 2.pow(numBits)
39+
)
3240
)
3341
or
3442
// Regular assignment: check if the value fits in the target type range

0 commit comments

Comments
 (0)