Skip to content

Commit 87e7013

Browse files
committed
EssentialTypes: Implement Rule 10.6
Adds a query which identifies "assignments" (as defined by MISRA C 2012) from composite expressions to objects of a wider essential type.
1 parent 931aa12 commit 87e7013

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @id c/misra/assignment-to-wider-essential-type
3+
* @name RULE-10-6: The value of a composite expression shall not be assigned to an object with wider essential type
4+
* @description
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-10-6
9+
* external/misra/obligation/required
10+
*/
11+
12+
import cpp
13+
import codingstandards.c.misra
14+
import codingstandards.c.misra.EssentialTypes
15+
import codingstandards.c.misra.MisraExpressions
16+
17+
from CompositeExpression ce, Type lValueType, Type compositeEssentialType
18+
where
19+
not isExcluded(ce, EssentialTypesPackage::assignmentToWiderEssentialTypeQuery()) and
20+
isAssignmentToEssentialType(lValueType, ce) and
21+
compositeEssentialType = getEssentialType(ce) and
22+
lValueType.getSize() > compositeEssentialType.getSize() and
23+
// Assignment to a different type category is prohibited by Rule 10.3, so we only report cases
24+
// where the assignment is to the same type category.
25+
getEssentialTypeCategory(lValueType) = getEssentialTypeCategory(compositeEssentialType)
26+
select ce, "Assignment to wider essential type: $@."
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:5:9:5:17 | ... + ... | Assignment to wider essential type: $@. |
2+
| test.c:7:24:7:32 | ... + ... | Assignment to wider essential type: $@. |
3+
| test.c:8:27:8:35 | ... + ... | Assignment to wider essential type: $@. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-10-6/AssignmentToWiderEssentialType.ql

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
void testWiderAssignment() {
2+
unsigned int u32 = 100;
3+
unsigned short u16 = 100;
4+
u16 = u16 + u16; // COMPLIANT
5+
u32 = u16 + u16; // NON_COMPLIANT
6+
u32 = (unsigned int)(u16 + u16); // COMPLIANT
7+
unsigned int u32_2 = u16 + u16; // NON_COMPLIANT
8+
unsigned int u32a[1] = {u16 + u16}; // NON_COMPLIANT
9+
10+
signed int s32 = u16 + u16; // ignored - prohibited by Rule 10.3
11+
}

0 commit comments

Comments
 (0)