Skip to content

Commit 46be7be

Browse files
committed
EssentialTypes: Implement Rule 10.8
Adds a query to check for inappropriate casts of composite expressions to wider essential types.
1 parent 70147d5 commit 46be7be

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @id c/misra/inappropriate-cast-of-composite-expression
3+
* @name RULE-10-8: The value of a composite expression shall not be cast to a different essential type category or a
4+
* @description The value of a composite expression shall not be cast to a different essential type
5+
* category or a wider essential type
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-10-8
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
import codingstandards.c.misra.EssentialTypes
16+
import codingstandards.c.misra.MisraExpressions
17+
18+
from
19+
Cast c, CompositeExpression ce, Type castEssentialType, Type compositeExprEssentialType,
20+
EssentialTypeCategory castTypeCategory, EssentialTypeCategory compositeTypeCategory,
21+
string message
22+
where
23+
not isExcluded(ce, EssentialTypesPackage::inappropriateCastOfCompositeExpressionQuery()) and
24+
c = ce.getExplicitlyConverted() and
25+
compositeExprEssentialType = getEssentialTypeBeforeConversions(ce) and
26+
castEssentialType = c.getType() and
27+
castTypeCategory = getEssentialTypeCategory(castEssentialType) and
28+
compositeTypeCategory = getEssentialTypeCategory(compositeExprEssentialType) and
29+
(
30+
not castTypeCategory = compositeTypeCategory and
31+
message =
32+
"Cast from " + compositeTypeCategory + " to " + castTypeCategory + " changes type category."
33+
or
34+
castTypeCategory = compositeTypeCategory and
35+
castEssentialType.getSize() > compositeExprEssentialType.getSize() and
36+
message = "Cast from " + compositeTypeCategory + " to " + castTypeCategory + " widens type."
37+
)
38+
select ce, message
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.c:4:16:4:20 | ... + ... | Cast from essentially Unsigned type to essentially Signed type changes type category. |
2+
| test.c:5:18:5:22 | ... + ... | Cast from essentially Signed type to essentially Unsigned type changes type category. |
3+
| test.c:14:18:14:24 | ... + ... | Cast from essentially Unsigned type to essentially Unsigned type widens type. |
4+
| test.c:20:16:20:22 | ... + ... | Cast from essentially Signed type to essentially Signed type widens type. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-10-8/InappropriateCastOfCompositeExpression.ql

c/misra/test/rules/RULE-10-8/test.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
void testDifferentEssentialType() {
2+
unsigned int u = 1;
3+
signed int s = 1;
4+
(signed int)(u + u); // NON_COMPLIANT
5+
(unsigned int)(s + s); // NON_COMPLIANT
6+
(signed int)(s + s); // COMPLIANT
7+
(unsigned int)(u + u); // COMPLIANT
8+
}
9+
10+
void testWiderType() {
11+
unsigned short us = 1;
12+
unsigned int u = 1;
13+
14+
(unsigned int)(us + us); // NON_COMPLIANT
15+
(unsigned short)(u + u); // COMPLIANT
16+
17+
signed short ss = 1;
18+
signed int s = 1;
19+
20+
(signed int)(ss + ss); // NON_COMPLIANT
21+
(signed short)(s + s); // COMPLIANT
22+
}

0 commit comments

Comments
 (0)