Skip to content

Commit 2147282

Browse files
committed
EssentialTypes: Implement Rule 10.2
Adds a query that detects inappropriate addition or subtraction operations on operands of essentially character type.
1 parent 7c5fea9 commit 2147282

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @id c/misra/addition-subtraction-on-essentially-char-type
3+
* @name RULE-10-2: Expressions of essentially character type shall not be used inappropriately in addition and
4+
* @description Expressions of essentially character type shall not be used inappropriately in
5+
* addition and subtraction operations
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-10-2
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
import codingstandards.c.misra.EssentialTypes
16+
17+
from BinaryArithmeticOperation addOrSub
18+
where
19+
not isExcluded(addOrSub, EssentialTypesPackage::additionSubtractionOnEssentiallyCharTypeQuery()) and
20+
addOrSub.getOperator() = ["+", "-"] and
21+
// At least one operand is essentially character type
22+
(
23+
getEssentialTypeCategory(getEssentialType(addOrSub.getLeftOperand())) =
24+
EssentiallyCharacterType() or
25+
getEssentialTypeCategory(getEssentialType(addOrSub.getRightOperand())) =
26+
EssentiallyCharacterType()
27+
) and
28+
not (
29+
// But the overall essential type is not essentially character type
30+
getEssentialTypeCategory(getEssentialType(addOrSub)) = EssentiallyCharacterType()
31+
or
32+
// Or this is a subtration of one character with another, which is permitted, but produces an integral type
33+
getEssentialTypeCategory(getEssentialType(addOrSub.getLeftOperand())) =
34+
EssentiallyCharacterType() and
35+
getEssentialTypeCategory(getEssentialType(addOrSub.getRightOperand())) =
36+
EssentiallyCharacterType() and
37+
addOrSub instanceof SubExpr
38+
)
39+
select addOrSub,
40+
"Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
| test.c:15:3:15:11 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
2+
| test.c:16:3:16:9 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
3+
| test.c:17:3:17:9 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
4+
| test.c:18:3:18:9 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
5+
| test.c:19:3:19:9 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
6+
| test.c:20:3:20:10 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
7+
| test.c:21:3:21:10 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
8+
| test.c:27:3:27:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
9+
| test.c:28:3:28:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
10+
| test.c:29:3:29:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
11+
| test.c:30:3:30:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
12+
| test.c:31:3:31:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
13+
| test.c:32:3:32:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
14+
| test.c:33:3:33:10 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
15+
| test.c:34:3:34:10 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-10-2/AdditionSubtractionOnEssentiallyCharType.ql

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdbool.h>
2+
3+
void testRules() {
4+
_Bool b = true;
5+
enum E1 { A, B, C } e1 = A;
6+
signed int i = 100;
7+
unsigned int u = 100;
8+
float f = 10.0f;
9+
10+
// Addition cases
11+
i + 'a'; // COMPLIANT
12+
'a' + i; // COMPLIANT
13+
u + 'a'; // COMPLIANT
14+
'a' + u; // COMPLIANT
15+
'a' + 'a'; // NON_COMPLIANT
16+
'a' + f; // NON_COMPLIANT
17+
f + 'a'; // NON_COMPLIANT
18+
'a' + b; // NON_COMPLIANT
19+
b + 'a'; // NON_COMPLIANT
20+
'a' + e1; // NON_COMPLIANT
21+
e1 + 'a'; // NON_COMPLIANT
22+
23+
// Subtration cases
24+
'a' - i; // COMPLIANT
25+
'a' - u; // COMPLIANT
26+
'a' - 'a'; // COMPLIANT
27+
'a' - f; // NON_COMPLIANT
28+
i - 'a'; // NON_COMPLIANT
29+
u - 'a'; // NON_COMPLIANT
30+
f - 'a'; // NON_COMPLIANT
31+
b - 'a'; // NON_COMPLIANT
32+
'a' - b; // NON_COMPLIANT
33+
e1 - 'a'; // NON_COMPLIANT
34+
'a' - e1; // NON_COMPLIANT
35+
}

0 commit comments

Comments
 (0)