Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita60ef7c

Browse files
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/implement-rule-amendments-tc2
2 parents3e6cf77 +9a1d770 commita60ef7c

File tree

24 files changed

+273
-48
lines changed

24 files changed

+273
-48
lines changed

‎amendments.csv‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ c,MISRA-C-2012,Amendment3,RULE-10-7,Yes,Refine,Yes,Import
1111
c,MISRA-C-2012,Amendment3,RULE-10-8,Yes,Refine,Yes,Import
1212
c,MISRA-C-2012,Amendment3,RULE-21-11,Yes,Clarification,Yes,Import
1313
c,MISRA-C-2012,Amendment3,RULE-21-12,Yes,Replace,No,Easy
14-
c,MISRA-C-2012,Amendment4,RULE-11-3,Yes,Expand,No,Easy
15-
c,MISRA-C-2012,Amendment4,RULE-11-8,Yes,Expand,No,Easy
16-
c,MISRA-C-2012,Amendment4,RULE-13-2,Yes,Expand,No,Very Hard
14+
c,MISRA-C-2012,Amendment4,RULE-11-3,Yes,Expand,Yes,Easy
15+
c,MISRA-C-2012,Amendment4,RULE-11-8,Yes,Expand,Yes,Easy
16+
c,MISRA-C-2012,Amendment4,RULE-13-2,Yes,Expand,Yes,Very Hard
1717
c,MISRA-C-2012,Amendment4,RULE-18-6,Yes,Expand,No,Medium
1818
c,MISRA-C-2012,Amendment4,RULE-18-8,Yes,Split,Yes,Easy
1919
c,MISRA-C-2012,Amendment4,RULE-2-2,Yes,Clarification,Yes,Import
2020
c,MISRA-C-2012,Amendment4,RULE-2-7,Yes,Clarification,Yes,Import
21-
c,MISRA-C-2012,Amendment4,RULE-3-1,Yes,Refine,No,Easy
21+
c,MISRA-C-2012,Amendment4,RULE-3-1,Yes,Refine,Yes,Easy
2222
c,MISRA-C-2012,Amendment4,RULE-8-6,Yes,Clarification,Yes,Import
2323
c,MISRA-C-2012,Amendment4,RULE-8-9,Yes,Clarification,Yes,Import
2424
c,MISRA-C-2012,Amendment4,RULE-9-4,Yes,Clarification,Yes,Import
2525
c,MISRA-C-2012,Amendment4,RULE-10-1,Yes,Clarification,Yes,Import
2626
c,MISRA-C-2012,Amendment4,RULE-18-3,Yes,Clarification,Yes,Import
2727
c,MISRA-C-2012,Amendment4,RULE-1-4,Yes,Replace,No,Easy
28-
c,MISRA-C-2012,Amendment4,RULE-9-1,Yes,Refine,No,Easy
28+
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
3131
c,MISRA-C-2012,Corrigendum2,RULE-7-4,Yes,Refine,Yes,Easy

‎c/common/test/rules/readofuninitializedmemory/test.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,6 @@ void test_non_default_init() {
9494
staticstructAss;
9595
use_struct_A(
9696
ss);// COMPLIANT - static struct type variables are zero initialized
97+
_Atomicintx;
98+
use_int(x);// COMPLIANT - atomics are special, covered by other rules
9799
}

‎c/misra/src/rules/RULE-11-3/CastBetweenObjectPointerAndDifferentObjectType.ql‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ where
2323
baseTypeFrom=cast.getExpr().getType().(PointerToObjectType).getBaseType()and
2424
baseTypeTo=cast.getType().(PointerToObjectType).getBaseType()and
2525
// exception: cast to a char, signed char, or unsigned char is permitted
26-
notbaseTypeTo.stripType()instanceofCharTypeand
26+
not(
27+
baseTypeTo.stripType()instanceofCharTypeand
28+
// Exception does not apply to _Atomic types
29+
notbaseTypeFrom.hasSpecifier("atomic")
30+
)and
2731
(
2832
(
2933
baseTypeFrom.isVolatile()andnotbaseTypeTo.isVolatile()

‎c/misra/src/rules/RULE-11-8/CastRemovesConstOrVolatileQualification.ql‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@ where
2424
baseTypeFrom.isVolatile()andnotbaseTypeTo.isVolatile()andqualificationName="volatile"
2525
or
2626
baseTypeFrom.isConst()andnotbaseTypeTo.isConst()andqualificationName="const"
27+
or
28+
baseTypeFrom.hasSpecifier("atomic")and
29+
notbaseTypeTo.hasSpecifier("atomic")and
30+
qualificationName="atomic"
2731
)
2832
selectcast,"Cast of pointer removes "+qualificationName+" qualification from its base type."
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @id c/misra/unsequenced-atomic-reads
3+
* @name RULE-13-2: The value of an atomic variable shall not depend on the evaluation order of interleaved threads
4+
* @description The value of an atomic variable shall not depend on evaluation order and
5+
* interleaving of threads.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-13-2
10+
* correctness
11+
* external/misra/c/2012/amendment3
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import semmle.code.cpp.dataflow.TaintTracking
17+
import codingstandards.c.misra
18+
import codingstandards.c.Ordering
19+
import codingstandards.c.orderofevaluation.VariableAccessOrdering
20+
21+
classAtomicAccessInFullExpressionOrderingextends Ordering::Configuration{
22+
AtomicAccessInFullExpressionOrdering(){this="AtomicAccessInFullExpressionOrdering"}
23+
24+
overridepredicateisCandidate(Expre1,Expre2){
25+
exists(AtomicVariableAccessa,AtomicVariableAccessb,FullExpre|a=e1andb=e2|
26+
a.getTarget()=b.getTarget()and
27+
a.(ConstituentExpr).getFullExpr()=eand
28+
b.(ConstituentExpr).getFullExpr()=eand
29+
nota=b
30+
)
31+
}
32+
}
33+
34+
/**
35+
* A read of a variable specified as `_Atomic`.
36+
*
37+
* Note, it may be accessed directly, or by passing its address into the std atomic functions.
38+
*/
39+
classAtomicVariableAccessextendsVariableAccess{
40+
AtomicVariableAccess(){getTarget().getType().hasSpecifier("atomic")}
41+
42+
/* Get the `atomic_<read|write>()` call this VarAccess occurs in. */
43+
FunctionCallgetAtomicFunctionCall(){
44+
exists(AddressOfExpraddrParent,FunctionCallfc|
45+
fc.getTarget().getName().matches("__c11_atomic%")and
46+
addrParent=fc.getArgument(0)and
47+
addrParent.getAnOperand()=thisand
48+
result=fc
49+
)
50+
}
51+
52+
/**
53+
* Gets an assigned expr, either in the form `x = <result>` or `atomic_store(&x, <result>)`.
54+
*/
55+
ExprgetAnAssignedExpr(){
56+
result=getAtomicFunctionCall().getArgument(1)
57+
or
58+
exists(AssignExprassign|
59+
assign.getLValue()=thisand
60+
result=assign.getRValue()
61+
)
62+
}
63+
64+
/**
65+
* Gets the expression holding this variable access, either in the form `x` or `atomic_read(&x)`.
66+
*/
67+
ExprgetARead(){
68+
result=getAtomicFunctionCall()
69+
or
70+
result=this
71+
}
72+
}
73+
74+
from
75+
AtomicAccessInFullExpressionOrderingconfig,FullExpre,Variablev,AtomicVariableAccessva1,
76+
AtomicVariableAccessva2
77+
where
78+
notisExcluded(e, SideEffects3Package::unsequencedAtomicReadsQuery())and
79+
e=va1.(ConstituentExpr).getFullExpr()and
80+
config.isUnsequenced(va1,va2)and
81+
v=va1.getTarget()and
82+
v=va2.getTarget()and
83+
// Exclude cases where the variable is assigned a value tainted by the other variable access.
84+
notexists(Exprwrite|
85+
write=va1.getAnAssignedExpr()and
86+
TaintTracking::localTaint(DataFlow::exprNode(va2.getARead()), DataFlow::exprNode(write))
87+
)and
88+
// Impose an ordering, show the first access.
89+
va1.getLocation().isBefore(va2.getLocation(), _)
90+
selecte,"Atomic variable $@ has a $@ that is unsequenced with $@.",v,v.getName(),va1,
91+
"previous read",va2,"another read"

‎c/misra/src/rules/RULE-3-1/CharacterSequencesAndUsedWithinAComment.ql‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,38 @@
1616
import cpp
1717
import codingstandards.c.misra
1818

19-
classIllegalCCommentCharacterextendsstring{
20-
IllegalCCommentCharacter(){
21-
this="/*"or
22-
this="//"
23-
}
19+
/* Character sequence is banned from all comment types */
20+
classIllegalCommentSequenceextendsstring{
21+
IllegalCommentSequence(){this="/*"}
2422
}
2523

26-
classIllegalCPPCommentCharacterextendsstring{
27-
IllegalCPPCommentCharacter(){this="/*"}
24+
/* A regexp to check for illegal C-style comments */
25+
classIllegalCCommentRegexpextendsstring{
26+
IllegalCCommentRegexp(){
27+
// Regexp to match "//" in C-style comments, which do not appear to be URLs. General format
28+
// uses negative lookahead/lookbehind to match like `.*(?<!HTTP:)//(?!GITHUB.).*`. Broken down
29+
// into parts:
30+
// - `.*PATTERN.*` - look for the pattern anywhere in the comment.
31+
// - `(?<![a-zA-Z]:)` - negative lookbehind, exclude "http://github.com" by seeing "p:".
32+
// - `//` - the actual illegal sequence.
33+
// - `(?!(pattern))` - negative lookahead, exclude "http://github.com" by seeing "github.".
34+
// - `[a-zA-Z0-9\\-]+\\\\.` - Assume alphanumeric/hyphen followed by '.' is a domain name.
35+
this=".*(?<![a-zA-Z]:)//(?![a-zA-Z0-9\\-]+\\\\.).*"
36+
}
37+
38+
stringgetDescription(){result="//"}
2839
}
2940

3041
fromCommentcomment,stringillegalSequence
3142
where
3243
notisExcluded(comment, SyntaxPackage::characterSequencesAndUsedWithinACommentQuery())and
3344
(
34-
exists(IllegalCCommentCharacterc|illegalSequence=c|
35-
comment.(CStyleComment).getContents().indexOf(illegalSequence)>0
45+
exists(IllegalCommentSequencec|illegalSequence=c|
46+
comment.getContents().indexOf(illegalSequence)>1
3647
)
3748
or
38-
exists(IllegalCPPCommentCharacterc|illegalSequence=c|
39-
comment.(CppStyleComment).getContents().indexOf(illegalSequence)>0
49+
exists(IllegalCCommentRegexpc|illegalSequence=c.getDescription()|
50+
comment.(CStyleComment).getContents().regexpMatch(c)
4051
)
4152
)
4253
selectcomment,"Comment contains an illegal sequence '"+illegalSequence+"'"

‎c/misra/test/rules/RULE-11-3/CastBetweenObjectPointerAndDifferentObjectType.expected‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@
66
| test.c:21:3:21:16 | (int *)... | Cast performed between a pointer to object type (char) and a pointer to a different object type (int). |
77
| test.c:22:20:22:21 | (int *)... | Cast performed between a pointer to object type (char) and a pointer to a different object type (int). |
88
| test.c:23:3:23:18 | (long long *)... | Cast performed between a pointer to object type (int) and a pointer to a different object type (long long). |
9+
| test.c:26:3:26:13 | (char *)... | Cast performed between a pointer to object type (_Atomic(int)) and a pointer to a different object type (char). |
10+
| test.c:27:8:27:10 | (char *)... | Cast performed between a pointer to object type (_Atomic(int)) and a pointer to a different object type (char). |
11+
| test.c:28:3:28:21 | (_Atomic(char) *)... | Cast performed between a pointer to object type (_Atomic(int)) and a pointer to a different object type (_Atomic(char)). |
12+
| test.c:29:23:29:25 | (_Atomic(char) *)... | Cast performed between a pointer to object type (_Atomic(int)) and a pointer to a different object type (_Atomic(char)). |

‎c/misra/test/rules/RULE-11-3/test.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,10 @@ void f1(void) {
2121
(int*const)v2;// NON_COMPLIANT
2222
int*constv10=v2;// NON_COMPLIANT
2323
(long long*)v10;// NON_COMPLIANT
24+
25+
_Atomicint*v11=0;
26+
(char*)v11;// NON_COMPLIANT
27+
v2=v11;// NON_COMPLIANT
28+
(_Atomicchar*)v11;// NON_COMPLIANT
29+
_Atomicchar*v12=v11;// NON_COMPLIANT
2430
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
| test.c:4:19:4:33 | (const char *)... | Cast of pointer removes volatile qualification from its base type. |
22
| test.c:6:13:6:21 | (char *)... | Cast of pointer removes const qualification from its base type. |
3+
| test.c:9:3:9:11 | (char *)... | Cast of pointer removes atomic qualification from its base type. |
4+
| test.c:10:7:10:7 | (char *)... | Cast of pointer removes atomic qualification from its base type. |
5+
| test.c:11:3:11:17 | (const char *)... | Cast of pointer removes atomic qualification from its base type. |
6+
| test.c:12:7:12:7 | (const char *)... | Cast of pointer removes atomic qualification from its base type. |

‎c/misra/test/rules/RULE-11-8/test.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@ int f1(void) {
55
constchar*c2= (constchar*)c;// COMPLIANT
66
char*d= (char*)c;// NON_COMPLIANT
77
constchar*e= (constchar*)d;// COMPLIANT
8+
_Atomicchar*f=0;
9+
(char*)f;// NON_COMPLIANT
10+
d=f;// NON_COMPLIANT
11+
(constchar*)f;// NON_COMPLIANT
12+
e=f;// NON_COMPLIANT
13+
(const _Atomicchar*)f;// COMPLIANT
14+
(const _Atomicchar*)f;// COMPLIANT
815
return0;
916
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp