Skip to content

Commit 2a6a85e

Browse files
committed
Declarations6: add RULE-8-11
1 parent e718559 commit 2a6a85e

File tree

7 files changed

+75
-2
lines changed

7 files changed

+75
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @id c/misra/array-external-linkage-size-explicitly-specified
3+
* @name RULE-8-11: When an array with external linkage is declared, its size should be explicitly specified
4+
* @description Declaring an array without an explicit size disallows the compiler and static
5+
* checkers from doing array bounds analysis and can lead to less readable, unsafe
6+
* code.
7+
* @kind problem
8+
* @precision very-high
9+
* @problem.severity error
10+
* @tags external/misra/id/rule-8-11
11+
* correctness
12+
* readability
13+
* external/misra/obligation/advisory
14+
*/
15+
16+
import cpp
17+
import codingstandards.c.misra
18+
import codingstandards.cpp.Identifiers
19+
20+
from VariableDeclarationEntry v, ArrayType t
21+
where
22+
not isExcluded(v, Declarations6Package::arrayExternalLinkageSizeExplicitlySpecifiedQuery()) and
23+
v.getDeclaration() instanceof ExternalIdentifiers and
24+
v.getType() = t and
25+
not exists(t.getSize()) and
26+
//this rule applies to non-defining declarations only
27+
not v.isDefinition()
28+
select v, "Array declared without explicit size."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:2:12:2:13 | declaration of a1 | Array declared without explicit size. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-11/ArrayExternalLinkageSizeExplicitlySpecified.ql

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern int a[1]; // COMPLIANT
2+
extern int a1[]; // NON_COMPLIANT
3+
extern int a2[] = {
4+
1}; // COMPLIANT - this rule applies to non-defining declarations only
5+
static int a3[]; // COMPLIANT - not external linkage
6+
int a4[]; // COMPLIANT - is a definition

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import codingstandards.cpp.exclusions.RuleMetadata
66
newtype Declarations6Query =
77
TFunctionDeclaredImplicitlyQuery() or
88
TIdentifiersWithExternalLinkageNotUniqueQuery() or
9-
TIdentifiersWithInternalLinkageNotUniqueQuery()
9+
TIdentifiersWithInternalLinkageNotUniqueQuery() or
10+
TArrayExternalLinkageSizeExplicitlySpecifiedQuery()
1011

1112
predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleId, string category) {
1213
query =
@@ -35,6 +36,15 @@ predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleI
3536
"c/misra/identifiers-with-internal-linkage-not-unique" and
3637
ruleId = "RULE-5-9" and
3738
category = "advisory"
39+
or
40+
query =
41+
// `Query` instance for the `arrayExternalLinkageSizeExplicitlySpecified` query
42+
Declarations6Package::arrayExternalLinkageSizeExplicitlySpecifiedQuery() and
43+
queryId =
44+
// `@id` for the `arrayExternalLinkageSizeExplicitlySpecified` query
45+
"c/misra/array-external-linkage-size-explicitly-specified" and
46+
ruleId = "RULE-8-11" and
47+
category = "advisory"
3848
}
3949

4050
module Declarations6Package {
@@ -58,4 +68,11 @@ module Declarations6Package {
5868
// `Query` type for `identifiersWithInternalLinkageNotUnique` query
5969
TQueryC(TDeclarations6PackageQuery(TIdentifiersWithInternalLinkageNotUniqueQuery()))
6070
}
71+
72+
Query arrayExternalLinkageSizeExplicitlySpecifiedQuery() {
73+
//autogenerate `Query` type
74+
result =
75+
// `Query` type for `arrayExternalLinkageSizeExplicitlySpecified` query
76+
TQueryC(TDeclarations6PackageQuery(TArrayExternalLinkageSizeExplicitlySpecifiedQuery()))
77+
}
6178
}

rule_packages/c/Declarations6.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@
5959
}
6060
],
6161
"title": "Identifiers that define objects or functions with internal linkage should be unique"
62+
},
63+
"RULE-8-11": {
64+
"properties": {
65+
"obligation": "advisory"
66+
},
67+
"queries": [
68+
{
69+
"description": "Declaring an array without an explicit size disallows the compiler and static checkers from doing array bounds analysis and can lead to less readable, unsafe code.",
70+
"kind": "problem",
71+
"name": "When an array with external linkage is declared, its size should be explicitly specified",
72+
"precision": "very-high",
73+
"severity": "error",
74+
"short_name": "ArrayExternalLinkageSizeExplicitlySpecified",
75+
"tags": [
76+
"correctness",
77+
"readability"
78+
]
79+
}
80+
],
81+
"title": "When an array with external linkage is declared, its size should be explicitly specified"
6282
}
6383
}
6484
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ c,MISRA-C-2012,RULE-8-7,Yes,Advisory,,,Functions and objects should not be defin
656656
c,MISRA-C-2012,RULE-8-8,Yes,Required,,,The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage,M3-3-2,Declarations,Medium,
657657
c,MISRA-C-2012,RULE-8-9,Yes,Advisory,,,An object should be defined at block scope if its identifier only appears in a single function,M3-4-1,Declarations,Medium,
658658
c,MISRA-C-2012,RULE-8-10,Yes,Required,,,An inline function shall be declared with the static storage class,,Declarations,Medium,
659-
c,MISRA-C-2012,RULE-8-11,Yes,Advisory,,,"When an array with external linkage is declared, its size should be explicitly specified",,Declarations,Medium,
659+
c,MISRA-C-2012,RULE-8-11,Yes,Advisory,,,"When an array with external linkage is declared, its size should be explicitly specified",,Declarations6,Medium,
660660
c,MISRA-C-2012,RULE-8-12,Yes,Required,,,"Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique",,Declarations,Medium,
661661
c,MISRA-C-2012,RULE-8-13,Yes,Advisory,,,A pointer should point to a const-qualified type whenever possible,,Pointers1,Medium,
662662
c,MISRA-C-2012,RULE-8-14,Yes,Required,,,The restrict type qualifier shall not be used,,Banned,Easy,

0 commit comments

Comments
 (0)