Skip to content

Commit 3d43eac

Browse files
authored
Merge pull request #707 from github/michaelrfairhurst/misra-c-rule-6-3-bit-fields-in-union
Add support for MISRA-C Rule 6.3
2 parents 7dfff4d + 9000deb commit 3d43eac

File tree

7 files changed

+97
-0
lines changed

7 files changed

+97
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @id c/misra/bit-field-declared-as-member-of-a-union
3+
* @name RULE-6-3: A bit field shall not be declared as a member of a union
4+
* @description Type punning on a union with bit fields relies on implementation-specific alignment
5+
* behavior.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-6-3
10+
* correctness
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
17+
from BitField field, Union u
18+
where
19+
not isExcluded(field, BitfieldTypes2Package::bitFieldDeclaredAsMemberOfAUnionQuery()) and
20+
u.getAField() = field
21+
select field,
22+
"Union member " + field.getName() +
23+
" is declared as a bit field which relies on implementation-specific behavior."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:7:7:7:7 | x | Union member x is declared as a bit field which relies on implementation-specific behavior. |
2+
| test.c:20:7:20:7 | (unnamed bitfield) | Union member (unnamed bitfield) is declared as a bit field which relies on implementation-specific behavior. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-6-3/BitFieldDeclaredAsMemberOfAUnion.ql

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

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
union U1 {
2+
int x; // COMPLIANT
3+
char y; // COMPLIANT
4+
};
5+
6+
union U2 {
7+
int x : 2; // NON-COMPLIANT
8+
char y; // COMPLIANT
9+
};
10+
11+
union U3 {
12+
struct str {
13+
int x : 4; // COMPLIANT
14+
int y : 2; // COMPLIANT
15+
};
16+
};
17+
18+
union U4 {
19+
char x;
20+
int : 0; // NON-COMPLIANT
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype BitfieldTypes2Query = TBitFieldDeclaredAsMemberOfAUnionQuery()
7+
8+
predicate isBitfieldTypes2QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `bitFieldDeclaredAsMemberOfAUnion` query
11+
BitfieldTypes2Package::bitFieldDeclaredAsMemberOfAUnionQuery() and
12+
queryId =
13+
// `@id` for the `bitFieldDeclaredAsMemberOfAUnion` query
14+
"c/misra/bit-field-declared-as-member-of-a-union" and
15+
ruleId = "RULE-6-3" and
16+
category = "required"
17+
}
18+
19+
module BitfieldTypes2Package {
20+
Query bitFieldDeclaredAsMemberOfAUnionQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `bitFieldDeclaredAsMemberOfAUnion` query
24+
TQueryC(TBitfieldTypes2PackageQuery(TBitFieldDeclaredAsMemberOfAUnionQuery()))
25+
}
26+
}

cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import codingstandards.cpp.exclusions.RuleMetadata
44
//** Import packages for this language **/
55
import Banned
66
import BitfieldTypes
7+
import BitfieldTypes2
78
import Concurrency1
89
import Concurrency2
910
import Concurrency3
@@ -75,6 +76,7 @@ import Types1
7576
newtype TCQuery =
7677
TBannedPackageQuery(BannedQuery q) or
7778
TBitfieldTypesPackageQuery(BitfieldTypesQuery q) or
79+
TBitfieldTypes2PackageQuery(BitfieldTypes2Query q) or
7880
TConcurrency1PackageQuery(Concurrency1Query q) or
7981
TConcurrency2PackageQuery(Concurrency2Query q) or
8082
TConcurrency3PackageQuery(Concurrency3Query q) or
@@ -146,6 +148,7 @@ newtype TCQuery =
146148
predicate isQueryMetadata(Query query, string queryId, string ruleId, string category) {
147149
isBannedQueryMetadata(query, queryId, ruleId, category) or
148150
isBitfieldTypesQueryMetadata(query, queryId, ruleId, category) or
151+
isBitfieldTypes2QueryMetadata(query, queryId, ruleId, category) or
149152
isConcurrency1QueryMetadata(query, queryId, ruleId, category) or
150153
isConcurrency2QueryMetadata(query, queryId, ruleId, category) or
151154
isConcurrency3QueryMetadata(query, queryId, ruleId, category) or

rule_packages/c/BitfieldTypes2.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"MISRA-C-2012": {
3+
"RULE-6-3": {
4+
"properties": {
5+
"obligation": "required"
6+
},
7+
"queries": [
8+
{
9+
"description": "Type punning on a union with bit fields relies on implementation-specific alignment behavior.",
10+
"kind": "problem",
11+
"name": "A bit field shall not be declared as a member of a union",
12+
"precision": "very-high",
13+
"severity": "warning",
14+
"short_name": "BitFieldDeclaredAsMemberOfAUnion",
15+
"tags": ["correctness"]
16+
}
17+
],
18+
"title": "A bit field shall not be declared as a member of a union"
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)