Skip to content

Commit a559f7a

Browse files
committed
Add RULE-11-6-1
1 parent d6a6d46 commit a559f7a

File tree

8 files changed

+120
-1
lines changed

8 files changed

+120
-1
lines changed
Lines changed: 26 additions & 0 deletions
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 Declarations7Query = TUninitializedVariableQuery()
7+
8+
predicate isDeclarations7QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `uninitializedVariable` query
11+
Declarations7Package::uninitializedVariableQuery() and
12+
queryId =
13+
// `@id` for the `uninitializedVariable` query
14+
"cpp/misra/uninitialized-variable" and
15+
ruleId = "RULE-11-6-1" and
16+
category = "advisory"
17+
}
18+
19+
module Declarations7Package {
20+
Query uninitializedVariableQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `uninitializedVariable` query
24+
TQueryCPP(TDeclarations7PackageQuery(TUninitializedVariableQuery()))
25+
}
26+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import Declarations1
3939
import Declarations2
4040
import Declarations3
4141
import Declarations4
42+
import Declarations7
4243
import ExceptionSafety
4344
import Exceptions1
4445
import Exceptions2
@@ -141,6 +142,7 @@ newtype TCPPQuery =
141142
TDeclarations2PackageQuery(Declarations2Query q) or
142143
TDeclarations3PackageQuery(Declarations3Query q) or
143144
TDeclarations4PackageQuery(Declarations4Query q) or
145+
TDeclarations7PackageQuery(Declarations7Query q) or
144146
TExceptionSafetyPackageQuery(ExceptionSafetyQuery q) or
145147
TExceptions1PackageQuery(Exceptions1Query q) or
146148
TExceptions2PackageQuery(Exceptions2Query q) or
@@ -243,6 +245,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
243245
isDeclarations2QueryMetadata(query, queryId, ruleId, category) or
244246
isDeclarations3QueryMetadata(query, queryId, ruleId, category) or
245247
isDeclarations4QueryMetadata(query, queryId, ruleId, category) or
248+
isDeclarations7QueryMetadata(query, queryId, ruleId, category) or
246249
isExceptionSafetyQueryMetadata(query, queryId, ruleId, category) or
247250
isExceptions1QueryMetadata(query, queryId, ruleId, category) or
248251
isExceptions2QueryMetadata(query, queryId, ruleId, category) or
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id cpp/misra/uninitialized-variable
3+
* @name RULE-11-6-1: All variables should be initialized, otherwise it may lead to a read of uninitialized memory which is undefined behavior.
4+
* @description All variables should be initialized
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-11-6-1
9+
* correctness
10+
* scope/single-translation-unit
11+
* external/misra/enforcement/decidable
12+
* external/misra/obligation/advisory
13+
*/
14+
15+
import cpp
16+
import codingstandards.cpp.misra
17+
import codingstandards.cpp.rules.readofuninitializedmemory.ReadOfUninitializedMemory
18+
19+
from UninitializedVariable v
20+
where not isExcluded(v, Declarations7Package::uninitializedVariableQuery())
21+
select v, "Uninitialized variable found."
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
problems
2+
#select
3+
| test.cpp:11:7:11:7 | i | Uninitialized variable found. |
4+
| test.cpp:18:8:18:9 | i4 | Uninitialized variable found. |
5+
| test.cpp:20:8:20:9 | p4 | Uninitialized variable found. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-11-6-1/UninitializedVariable.ql
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <string>
2+
3+
int g; // COMPLIANT
4+
5+
class A {
6+
public:
7+
int m1;
8+
};
9+
10+
void f(int p) { // COMPLIANT - not applicable to parameters
11+
int i; // NON_COMPLIANT
12+
int i1 = 0; // COMPLIANT
13+
int i2[10] = {1, 0}; // COMPLIANT
14+
int i3[10]; // COMPLIANT
15+
A a; // COMPLIANT - default initialized
16+
A a1;
17+
a1.m1 = 1; // COMPLIANT
18+
int *i4 = new int; // NON_COMPLIANT
19+
int *i5 = new int(1); // COMPLIANT
20+
int *p4; // NON_COMPLIANT
21+
p4 = new int;
22+
std::string s; // COMPLIANT
23+
}
24+
25+
void test_non_default_init() {
26+
static int sl; // COMPLIANT - static variables are zero initialized
27+
thread_local int
28+
tl; // COMPLIANT - thread local variables are zero initialized
29+
static int *slp; // COMPLIANT - static variables are zero initialized
30+
thread_local int
31+
*tlp; // COMPLIANT - thread local variables are zero initialized
32+
_Atomic int
33+
ai; // COMPLIANT - atomics are special and not covered by this rule
34+
}
35+
36+
namespace {
37+
int i; // COMPLIANT
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"MISRA-C++-2023": {
3+
"RULE-11-6-1": {
4+
"properties": {
5+
"enforcement": "decidable",
6+
"obligation": "advisory"
7+
},
8+
"queries": [
9+
{
10+
"description": "All variables should be initialized",
11+
"kind": "problem",
12+
"name": "All variables should be initialized, otherwise it may lead to a read of uninitialized memory which is undefined behavior.",
13+
"precision": "very-high",
14+
"severity": "error",
15+
"short_name": "UninitializedVariable",
16+
"tags": [
17+
"correctness",
18+
"scope/single-translation-unit"
19+
]
20+
}
21+
],
22+
"title": "All variables should be initialized"
23+
}
24+
}
25+
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ cpp,MISRA-C++-2023,RULE-6-0-3,Yes,Advisory,Decidable,Single Translation Unit,"Th
855855
cpp,MISRA-C++-2023,RULE-6-0-4,Yes,Required,Decidable,Single Translation Unit,The identifier main shall not be used for a function other than the global function main,M7-3-2,ImportMisra23,Import,
856856
cpp,MISRA-C++-2023,RULE-6-2-1,Yes,Required,Decidable,System,The one-definition rule shall not be violated,M3-2-2,ImportMisra23,Import,
857857
cpp,MISRA-C++-2023,RULE-6-2-2,Yes,Required,Decidable,System,All declarations of a variable or function shall have the same type,"M3-9-1,DCL40-C",Declarations2,Easy,
858-
cpp,MISRA-C++-2023,RULE-6-2-3,Yes,Required,Decidable,System,The source code used to implement an entity shall appear only once,,Declarations7,Medium,
858+
cpp,MISRA-C++-2023,RULE-6-2-3,Yes,Required,Decidable,System,The source code used to implement an entity shall appear only once,,Declarations8,Medium,
859859
cpp,MISRA-C++-2023,RULE-6-2-4,Yes,Required,Decidable,Single Translation Unit,A header file shall not contain definitions of functions or objects that are non-inline and have external linkage,A3-1-1,Linkage2,Import,
860860
cpp,MISRA-C++-2023,RULE-6-4-1,Yes,Required,Decidable,Single Translation Unit,A variable declared in an inner scope shall not hide a variable declared in an outer scope,A2-10-1,ImportMisra23,Import,
861861
cpp,MISRA-C++-2023,RULE-6-4-2,Yes,Required,Decidable,Single Translation Unit,Derived classes shall not conceal functions that are inherited from their bases,A7-3-1,ImportMisra23,Import,

0 commit comments

Comments
 (0)