Skip to content

Commit 6d7395a

Browse files
authored
Merge pull request #841 from github/michaelrfairhurst/implement-rule-amendments-tc2
Implement rule amendments from Technical Corrigenda 2
2 parents 9a1d770 + a60ef7c commit 6d7395a

File tree

21 files changed

+345
-80
lines changed

21 files changed

+345
-80
lines changed

amendments.csv

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ c,MISRA-C-2012,Amendment4,RULE-1-4,Yes,Replace,No,Easy
2828
c,MISRA-C-2012,Amendment4,RULE-9-1,Yes,Refine,Yes,Easy
2929
c,MISRA-C-2012,Amendment4,RULE-9-2,Yes,Refine,No,Import
3030
c,MISRA-C-2012,Corrigendum2,DIR-4-10,Yes,Clarification,Yes,Import
31-
c,MISRA-C-2012,Corrigendum2,RULE-7-4,Yes,Refine,No,Easy
31+
c,MISRA-C-2012,Corrigendum2,RULE-7-4,Yes,Refine,Yes,Easy
3232
c,MISRA-C-2012,Corrigendum2,RULE-8-2,Yes,Clarification,Yes,Import
33-
c,MISRA-C-2012,Corrigendum2,RULE-8-3,Yes,Refine,No,Easy
33+
c,MISRA-C-2012,Corrigendum2,RULE-8-3,Yes,Refine,Yes,Easy
3434
c,MISRA-C-2012,Corrigendum2,RULE-8-7,Yes,Clarification,Yes,Import
3535
c,MISRA-C-2012,Corrigendum2,RULE-10-1,Yes,Clarification,Yes,Import
36-
c,MISRA-C-2012,Corrigendum2,RULE-10-2,Yes,Refine,No,Easy
36+
c,MISRA-C-2012,Corrigendum2,RULE-10-2,Yes,Refine,Yes,Easy
3737
c,MISRA-C-2012,Corrigendum2,RULE-10-3,Yes,Clarification,Yes,Import
3838
c,MISRA-C-2012,Corrigendum2,RULE-11-3,Yes,Clarification,Yes,Import
3939
c,MISRA-C-2012,Corrigendum2,RULE-11-6,Yes,Clarification,Yes,Import
4040
c,MISRA-C-2012,Corrigendum2,RULE-13-2,Yes,Clarification,Yes,Import
4141
c,MISRA-C-2012,Corrigendum2,RULE-13-6,Yes,Clarification,Yes,Import
42-
c,MISRA-C-2012,Corrigendum2,RULE-14-3,Yes,Refine,No,Easy
42+
c,MISRA-C-2012,Corrigendum2,RULE-14-3,Yes,Refine,Yes,Easy
4343
c,MISRA-C-2012,Corrigendum2,RULE-15-7,Yes,Clarification,Yes,Import
4444
c,MISRA-C-2012,Corrigendum2,RULE-17-4,Yes,Clarification,Yes,Import
4545
c,MISRA-C-2012,Corrigendum2,RULE-17-5,Yes,Clarification,Yes,Import
46-
c,MISRA-C-2012,Corrigendum2,RULE-18-1,Yes,Refine,No,Easy
46+
c,MISRA-C-2012,Corrigendum2,RULE-18-1,Yes,Refine,Yes,Easy
4747
c,MISRA-C-2012,Corrigendum2,RULE-20-14,No,Clarification,Yes,Import
4848
c,MISRA-C-2012,Corrigendum2,RULE-21-19,Yes,Clarification,Yes,Import
49-
c,MISRA-C-2012,Corrigendum2,RULE-21-20,Yes,Refine,No,Easy
50-
c,MISRA-C-2012,Corrigendum2,RULE-22-9,Yes,Clarification,Yes,Import
49+
c,MISRA-C-2012,Corrigendum2,RULE-21-20,Yes,Refine,Yes,Easy
50+
c,MISRA-C-2012,Corrigendum2,RULE-22-9,Yes,Clarification,Yes,Import
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| test.c:4:13:4:18 | ... + ... | Array pointer p2 points 1 element passed the end of $@. | test.c:2:7:2:8 | l1 | l1 |
2-
| test.c:5:13:5:18 | ... + ... | Array pointer p3 points 1 element passed the end of $@. | test.c:2:7:2:8 | l1 | l1 |
3-
| test.c:6:13:6:18 | & ... | Array pointer p4 points 1 element passed the end of $@. | test.c:2:7:2:8 | l1 | l1 |
4-
| test.c:11:8:11:11 | ... -- | Array pointer p7 points 1 element passed the end of $@. | test.c:2:7:2:8 | l1 | l1 |
5-
| test.c:12:8:12:9 | p3 | Array pointer p8 points 1 element passed the end of $@. | test.c:2:7:2:8 | l1 | l1 |
1+
| test.c:4:13:4:18 | ... + ... | Array pointer p2 points 1 element past the end of $@. | test.c:2:7:2:8 | l1 | l1 |
2+
| test.c:5:13:5:18 | ... + ... | Array pointer p3 points 1 element past the end of $@. | test.c:2:7:2:8 | l1 | l1 |
3+
| test.c:6:13:6:18 | & ... | Array pointer p4 points 1 element past the end of $@. | test.c:2:7:2:8 | l1 | l1 |
4+
| test.c:11:8:11:11 | ... -- | Array pointer p7 points 1 element past the end of $@. | test.c:2:7:2:8 | l1 | l1 |
5+
| test.c:12:8:12:9 | p3 | Array pointer p8 points 1 element past the end of $@. | test.c:2:7:2:8 | l1 | l1 |

c/misra/src/codingstandards/c/misra/EssentialTypes.qll

+18-9
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,14 @@ class EssentialBinaryOperationSubjectToUsualConversions extends EssentialExpr, B
328328
exists(
329329
Type leftEssentialType, Type rightEssentialType,
330330
EssentialTypeCategory leftEssentialTypeCategory,
331-
EssentialTypeCategory rightEssentialTypeCategory
331+
EssentialTypeCategory rightEssentialTypeCategory, int intTypeSize
332332
|
333333
leftEssentialType = getEssentialType(getLeftOperand()) and
334334
rightEssentialType = getEssentialType(getRightOperand()) and
335335
leftEssentialTypeCategory = getEssentialTypeCategory(leftEssentialType) and
336-
rightEssentialTypeCategory = getEssentialTypeCategory(rightEssentialType)
336+
rightEssentialTypeCategory = getEssentialTypeCategory(rightEssentialType) and
337+
// For rules around addition/subtraction with char types:
338+
intTypeSize = any(IntType i | i.isSigned()).getSize()
337339
|
338340
if
339341
leftEssentialTypeCategory = rightEssentialTypeCategory and
@@ -356,14 +358,18 @@ class EssentialBinaryOperationSubjectToUsualConversions extends EssentialExpr, B
356358
class EssentialAddExpr extends EssentialBinaryOperationSubjectToUsualConversions, AddExpr {
357359
override Type getEssentialType() {
358360
exists(
359-
EssentialTypeCategory operandTypeCategory, EssentialTypeCategory otherOperandTypeCategory
361+
Type otherOperandType, EssentialTypeCategory operandTypeCategory,
362+
EssentialTypeCategory otherOperandTypeCategory, int intTypeSize
360363
|
361364
operandTypeCategory = getEssentialTypeCategory(getEssentialType(getAnOperand())) and
362-
otherOperandTypeCategory = getEssentialTypeCategory(getEssentialType(getAnOperand()))
365+
otherOperandType = getEssentialType(getAnOperand()) and
366+
otherOperandTypeCategory = getEssentialTypeCategory(otherOperandType) and
367+
intTypeSize = any(IntType i).getSize()
363368
|
364369
if
365370
operandTypeCategory = EssentiallyCharacterType() and
366-
otherOperandTypeCategory instanceof EssentiallySignedOrUnsignedType
371+
otherOperandTypeCategory instanceof EssentiallySignedOrUnsignedType and
372+
otherOperandType.getSize() <= intTypeSize
367373
then result instanceof PlainCharType
368374
else result = super.getEssentialType()
369375
)
@@ -376,15 +382,18 @@ class EssentialAddExpr extends EssentialBinaryOperationSubjectToUsualConversions
376382
class EssentialSubExpr extends EssentialBinaryOperationSubjectToUsualConversions, SubExpr {
377383
override Type getEssentialType() {
378384
exists(
379-
EssentialTypeCategory leftEssentialTypeCategory,
380-
EssentialTypeCategory rightEssentialTypeCategory
385+
EssentialTypeCategory leftEssentialTypeCategory, Type rightEssentialType,
386+
EssentialTypeCategory rightEssentialTypeCategory, int intTypeSize
381387
|
382388
leftEssentialTypeCategory = getEssentialTypeCategory(getEssentialType(getLeftOperand())) and
383-
rightEssentialTypeCategory = getEssentialTypeCategory(getEssentialType(getRightOperand()))
389+
rightEssentialType = getEssentialType(getRightOperand()) and
390+
rightEssentialTypeCategory = getEssentialTypeCategory(rightEssentialType) and
391+
intTypeSize = any(IntType i).getSize()
384392
|
385393
if
386394
leftEssentialTypeCategory = EssentiallyCharacterType() and
387-
rightEssentialTypeCategory instanceof EssentiallySignedOrUnsignedType
395+
rightEssentialTypeCategory instanceof EssentiallySignedOrUnsignedType and
396+
rightEssentialType.getSize() <= intTypeSize
388397
then result instanceof PlainCharType
389398
else result = super.getEssentialType()
390399
)

c/misra/src/rules/RULE-10-2/AdditionSubtractionOnEssentiallyCharType.ql

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ where
3232
// But the overall essential type is not essentially character type
3333
getEssentialTypeCategory(getEssentialType(addOrSub)) = EssentiallyCharacterType()
3434
or
35-
// Or this is a subtration of one character with another, which is permitted, but produces an integral type
35+
// Or this is a subtraction of one character with another, which is permitted, but produces an integral type
3636
getEssentialTypeCategory(getEssentialType(addOrSub.getLeftOperand())) =
3737
EssentiallyCharacterType() and
3838
getEssentialTypeCategory(getEssentialType(addOrSub.getRightOperand())) =
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
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 |
1+
| test.c:19:3:19:11 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
2+
| test.c:20:3:20:9 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
3+
| test.c:21:3:21:9 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
4+
| test.c:22:3:22:9 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
5+
| test.c:23:3:23:9 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
6+
| test.c:24:3:24:10 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
7+
| test.c:25:3:25:10 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
8+
| test.c:28:3:28:9 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
9+
| test.c:29:3:29:10 | ... + ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
10+
| test.c:35:3:35:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
11+
| test.c:36:3:36:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
12+
| test.c:37:3:37:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
13+
| test.c:38:3:38:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
14+
| test.c:39:3:39:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
15+
| test.c:40:3:40:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
16+
| test.c:41:3:41:10 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
17+
| test.c:42:3:42:10 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
18+
| test.c:45:3:45:9 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |
19+
| test.c:46:3:46:10 | ... - ... | Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations |

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ void testRules() {
55
enum E1 { A, B, C } e1 = A;
66
signed int i = 100;
77
unsigned int u = 100;
8+
signed short s = 100;
9+
unsigned short us = 100;
10+
signed long l = 100L;
11+
unsigned long ul = 100UL;
812
float f = 10.0f;
913

1014
// Addition cases
@@ -19,8 +23,12 @@ void testRules() {
1923
b + 'a'; // NON_COMPLIANT
2024
'a' + e1; // NON_COMPLIANT
2125
e1 + 'a'; // NON_COMPLIANT
26+
'a' + s; // COMPLIANT
27+
'a' + us; // COMPLIANT
28+
'a' + l; // NON_COMPLIANT
29+
'a' + ul; // NON_COMPLIANT
2230

23-
// Subtration cases
31+
// Subtraction cases
2432
'a' - i; // COMPLIANT
2533
'a' - u; // COMPLIANT
2634
'a' - 'a'; // COMPLIANT
@@ -32,4 +40,8 @@ void testRules() {
3240
'a' - b; // NON_COMPLIANT
3341
e1 - 'a'; // NON_COMPLIANT
3442
'a' - e1; // NON_COMPLIANT
43+
'a' - s; // COMPLIANT
44+
'a' - us; // COMPLIANT
45+
'a' - l; // NON_COMPLIANT
46+
'a' - ul; // NON_COMPLIANT
3547
}

c/misra/test/rules/RULE-14-3/ControllingExprInvariant.expected

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
| test.c:27:10:27:14 | ... < ... | Controlling expression in loop statement has an invariant value. |
66
| test.c:37:3:37:6 | 1 | Controlling expression in conditional statement has an invariant value. |
77
| test.c:38:3:38:3 | 1 | Controlling expression in conditional statement has an invariant value. |
8+
| test.c:45:10:45:26 | ... && ... | Controlling expression in loop statement has an invariant value. |

c/misra/test/rules/RULE-14-3/test.c

+7
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ void f5(bool b1) {
3737
true ? 1 : 2; // NON_COMPLIANT
3838
1 ? 1 : 2; // NON_COMPLIANT
3939
b1 ? 1 : 2; // COMPLIANT
40+
}
41+
42+
void f6(int p1) {
43+
while (p1 < 10 && p1 > 12) { // NON_COMPLIANT[FALSE_NEGATIVE]
44+
}
45+
while (1 == 0 && p1 > 12) { // NON_COMPLIANT
46+
}
4047
}

c/misra/test/rules/RULE-7-4/StringLiteralAssignedToNonConstChar.expected

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
| test.c:58:5:58:22 | return ... | wchar_t * function w_sample3 is returning a string literal. |
77
| test.c:69:3:69:9 | call to sample4 | char * parameter of sample4 is passed a string literal. |
88
| test.c:78:3:78:11 | call to w_sample4 | wchar_t * parameter of w_sample4 is passed a string literal. |
9+
| test.c:91:3:91:11 | call to w_sample7 | char * parameter of w_sample7 is passed a string literal. |

c/misra/test/rules/RULE-7-4/test.c

+12
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ void w_call45() {
7979
w_sample5(L"string9"); // COMPLIANT: passing string literal to const char*
8080
}
8181

82+
void w_sample6(int x, ...) {}
83+
84+
void w_call6() {
85+
w_sample6(1, "string10"); // COMPLIANT by first (and only) exception
86+
}
87+
88+
void w_sample7(char *x, ...) {}
89+
90+
void w_call7() {
91+
w_sample7("string11", 1); // NON_COMPLIANT, does not fit exceptional case
92+
}
93+
8294
int main() { return 0; }

c/misra/test/rules/RULE-8-3/function1.c

+4
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ a f21(wi w, wi h) { // NON_COMPLIANT
2424

2525
void f22(int f22b, int f22a) { // NON_COMPLIANT
2626
return;
27+
}
28+
29+
void f23(int f23a) { // COMPLIANT
30+
return;
2731
}

c/misra/test/rules/RULE-8-3/function2.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ typedef long a;
1616

1717
extern a f21(wi w, hi h); // NON_COMPLIANT
1818

19-
extern void f22(int f22a, int f22b); // NON_COMPLIANT
19+
extern void f22(int f22a, int f22b); // NON_COMPLIANT
20+
21+
extern void f23(int); // COMPLIANT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- `RULE-8-3` - `DeclarationsOfAFunctionSameNameAndType.ql`:
2+
- Implement new exception, unnamed parameters are not covered by this rule.
3+
- `RULE-10-2` - `AdditionSubtractionOnEssentiallyCharType.ql`:
4+
- Disallow `+` and `-` operations with an essentially char type and other types larger than int type.
5+
- Note, this change affects the essential type of such expressions, which may affect other essential types rules.
6+
- `RULE-18-1`, `M5-0-16` - `PointerAndDerivedPointerMustAddressSameArray.ql`, `PointerAndDerivedPointerAccessDifferentArray.ql`:
7+
- Treat casts to byte pointers as pointers to arrays of the size of the pointed-to type.
8+
- Fix typo in report message, "passed" replaced with "past."
9+
- Suppress results where range analysis appears potentially unreliable.
10+
- `RULE-21-10`, `RULE-25-5-3`, `ENV34-C` - `CallToSetlocaleInvalidatesOldPointers.ql`, `CallToSetlocaleInvalidatesOldPointersMisra.ql`, `DoNotStorePointersReturnedByEnvFunctions.ql`:
11+
- Report usage of returned pointers from `asctime`, `ctime`, during a call to either of the former.
12+
- Report usage of returned pointers from `gmtime`, `localtime`, during a call to either of the former.

cpp/common/src/codingstandards/cpp/Compatible.qll

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ predicate parameterTypesIncompatible(FunctionDeclarationEntry f1, FunctionDeclar
2121

2222
predicate parameterNamesIncompatible(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) {
2323
f1.getDeclaration() = f2.getDeclaration() and
24-
exists(ParameterDeclarationEntry p1, ParameterDeclarationEntry p2, int i |
25-
p1 = f1.getParameterDeclarationEntry(i) and
26-
p2 = f2.getParameterDeclarationEntry(i)
24+
exists(string p1Name, string p2Name, int i |
25+
p1Name = f1.getParameterDeclarationEntry(i).getName() and
26+
p2Name = f2.getParameterDeclarationEntry(i).getName()
2727
|
28-
not p1.getName() = p2.getName()
28+
not p1Name = p2Name
2929
)
3030
}

0 commit comments

Comments
 (0)