Skip to content

Commit 94cfcda

Browse files
committed
MisraType: Avoid misuse of getSize()
1 parent bcf8ae9 commit 94cfcda

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

‎cpp/misra/src/codingstandards/cpp/misra/BuiltInTypeRules.qll‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ newtype Signedness =
9393
* - Typedefs to built in types
9494
* - References to built in types
9595
* - Enum types with an explicit underlying type that is a built-in type.
96+
*
97+
* Note: this does not extend `Type` directly, to prevent accidental use of `getSize()`, which
98+
* returns the "wrong" size for e.g. reference types.
9699
*/
97-
classMisraBuiltInTypeextendsType{
100+
classMisraBuiltInTypeextendsElement{
98101
// The built in type underlying this MISRA built in type
99102
BuiltInTypebuiltInType;
100103

@@ -108,6 +111,8 @@ class MisraBuiltInType extends Type{
108111
TypeCategorygetTypeCategory(){result=getBuiltInTypeCategory(builtInType)}
109112

110113
predicateisSameType(MisraBuiltInTypeother){this.getBuiltInType()=other.getBuiltInType()}
114+
115+
stringgetName(){result=this.(Type).getName()}
111116
}
112117

113118
classCharacterTypeextendsMisraBuiltInType{
@@ -298,13 +303,13 @@ CanonicalIntegerNumericType getBitFieldType(BitField bf){
298303
bitfieldActualType=bf.getType()and
299304
// Integral type with the same signedness as the bit field, and big enough to hold the bit field value
300305
result.getSignedness()=bitfieldActualType.getSignedness()and
301-
result.getSize()*8>=bf.getNumBits()and
306+
result.getBuiltInSize()*8>=bf.getNumBits()and
302307
// No smaller integral type can hold the bit field value
303308
notexists(CanonicalIntegerNumericTypeother|
304-
other.getSize()*8>=bf.getNumBits()and
309+
other.getBuiltInSize()*8>=bf.getNumBits()and
305310
other.getSignedness()=result.getSignedness()
306311
|
307-
other.getSize()<result.getBuiltInSize()
312+
other.getBuiltInSize()<result.getBuiltInSize()
308313
)
309314
)
310315
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ predicate isSignedConstantLeftShiftException(LShiftExpr shift){
4545
leftVal=left.getValue().toBigInt()and
4646
rightVal=right.getValue().toInt()and
4747
leftVal>=0.toBigInt()and
48-
maxBit=leftType.getSize()*8-1and
48+
maxBit=leftType.getBuiltInSize()*8-1and
4949
// Check that no set bit is shifted into or beyond the sign bit
5050
leftVal*2.toBigInt().pow(rightVal)<2.toBigInt().pow(maxBit)
5151
)

0 commit comments

Comments
(0)