Skip to content

Commit c29ca80

Browse files
committed
Add lifetime rule RULE-11-6-2
also adjust formatting on prev changenote which was incorrect
1 parent 262768a commit c29ca80

File tree

10 files changed

+164
-4
lines changed

10 files changed

+164
-4
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
"Language1",
260260
"Language2",
261261
"Language3",
262+
"Lifetime",
262263
"Linkage1",
263264
"Linkage2",
264265
"Literals",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
- `A3-1-1` - `ViolationsOfOneDefinitionRule.ql`:
22
- The query previously would incorrectly allow cases where something was defined with `extern` and did not use the defined external linkage library to find external linkage. This change may result in the query finding more results. Additionally a typo has been fixed in the alert message which will cause the old alerts for this query to now show up as new ones.
3-
- `RULE-6-0-2`, `A3-1-4` - `ExternalLinkageArrayWithoutExplicitSizeMisra.ql`, `ExternalLinkageArrayWithoutExplicitSizeAutosar.ql`:
3+
- `RULE-6-0-2`, `A3-1-4` - `ExternalLinkageArrayWithoutExplicitSizeMisra.ql`, `ExternalLinkageArrayWithoutExplicitSizeAutosar.ql`:
44
- The queries listed now find flexible member arrays in structs, as those do not have an explicit size.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A8-5-0`, `EXP53-CPP`, `EXP33-C`, `RULE-9-1` - `MemoryNotInitializedBeforeItIsRead.ql`, `DoNotReadUninitializedMemory.ql`, `DoNotReadUninitializedMemory.ql`, `ObjectWithAutoStorageDurationReadBeforeInit.ql`:
2+
- The queries listed now find uses of the operator 'new' where there is no value initialization provided.

cpp/common/src/codingstandards/cpp/rules/readofuninitializedmemory/ReadOfUninitializedMemory.qll

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import codingstandards.cpp.Customizations
77
import codingstandards.cpp.Exclusions
88
import semmle.code.cpp.controlflow.Guards
99
import semmle.code.cpp.controlflow.SubBasicBlocks
10+
import codingstandards.cpp.enhancements.AggregateLiteralEnhancements
1011

1112
abstract class ReadOfUninitializedMemorySharedQuery extends Query { }
1213

@@ -126,8 +127,18 @@ class InitializationContext extends TInitializationContext {
126127
*/
127128
class UninitializedVariable extends LocalVariable {
128129
UninitializedVariable() {
129-
// Not initialized at declaration
130-
not exists(getInitializer()) and
130+
(
131+
// Not initialized at declaration
132+
not exists(getInitializer())
133+
or
134+
//or is a builtin type used with new operator but there is no value initialization as far as we can see
135+
exists(Initializer i, NewExpr n |
136+
i = getInitializer() and
137+
n = i.getExpr() and
138+
this.getUnspecifiedType().stripType() instanceof BuiltInType and
139+
not i.isBraced()
140+
)
141+
) and
131142
// Not static or thread local, because they are not initialized with indeterminate values
132143
not isStatic() and
133144
not isThreadLocal() and

cpp/common/test/rules/readofuninitializedmemory/ReadOfUninitializedMemory.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
| test.cpp:39:7:39:8 | l3 | Local variable $@ is read here and may not be initialized on all paths. | test.cpp:38:6:38:7 | l3 | l3 |
44
| test.cpp:86:9:86:16 | arrayPtr | Local variable $@ is read here and may not be initialized on all paths. | test.cpp:79:8:79:15 | arrayPtr | arrayPtr |
55
| test.cpp:93:9:93:10 | l5 | Local variable $@ is read here and may not be initialized on all paths. | test.cpp:89:7:89:8 | l5 | l5 |
6+
| test.cpp:134:11:134:11 | i | Local variable $@ is read here and may not be initialized on all paths. | test.cpp:133:7:133:7 | i | i |
7+
| test.cpp:137:13:137:14 | i1 | Local variable $@ is read here and may not be initialized on all paths. | test.cpp:136:8:136:9 | i1 | i1 |
8+
| test.cpp:141:12:141:13 | i1 | Local variable $@ is read here and may not be initialized on all paths. | test.cpp:136:8:136:9 | i1 | i1 |

cpp/common/test/rules/readofuninitializedmemory/test.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,75 @@ void test_non_default_init() {
123123
use(tlp); // COMPLIANT - thread local variables are zero initialized
124124
_Atomic int ai;
125125
use(ai); // COMPLIANT - atomics are special and not covered by this rule
126+
}
127+
128+
namespace {
129+
int i; // COMPLIANT
130+
}
131+
132+
void extra_test() {
133+
int i;
134+
int j = i + 1; // NON_COMPLIANT
135+
136+
int *i1 = new int;
137+
int i2 = *i1; // NON_COMPLIANT
138+
139+
int *i3;
140+
141+
if (i3 = i1) { // NON_COMPLIANT
142+
}
143+
}
144+
145+
void extra_conditionals(bool b) {
146+
if (b) {
147+
goto L;
148+
}
149+
int i;
150+
i = 1;
151+
L:
152+
i = i + 1; // NON_COMPLIANT[FALSE_NEGATIVE]
153+
}
154+
155+
struct S {
156+
int m1;
157+
int m2;
158+
};
159+
160+
void struct_test() {
161+
S s1;
162+
S s2 = {1};
163+
164+
auto i1 = s1.m1; // NON_COMPLIANT[FALSE_NEGATIVE] - rule currently is not
165+
// field sensitive
166+
auto i2 = s2.m2; // COMPLIANT
167+
168+
int a1[10] = {1, 1, 1};
169+
int a2[10];
170+
171+
auto a3 = a1[5]; // COMPLIANT
172+
auto a4 = a2[5]; // NON_COMPLIANT[FALSE_NEGATIVE]
173+
}
174+
175+
class C {
176+
private:
177+
int m1;
178+
int m2;
179+
180+
public:
181+
C() : m1(1), m2(1) {}
182+
183+
C(int a) : m1(a) {}
184+
185+
int getm2() { return m2; }
186+
};
187+
188+
void test_class() {
189+
C c1;
190+
if (c1.getm2() > 0) { // COMPLIANT
191+
}
192+
193+
C c2(5);
194+
if (c2.getm2() > 0) { // NON_COMPLIANT[FALSE_NEGATIVE] - rule currently is not
195+
// field sensitive
196+
}
126197
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id cpp/misra/value-of-an-object-must-not-be-read-before-it-has-been-set
3+
* @name RULE-11-6-2: The value of an object must not be read before it has been set
4+
* @description Reading from uninitialized indeterminate values may produce undefined behavior.
5+
* @kind problem
6+
* @precision medium
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-11-6-2
9+
* correctness
10+
* security
11+
* scope/system
12+
* external/misra/enforcement/undecidable
13+
* external/misra/obligation/mandatory
14+
*/
15+
16+
import cpp
17+
import codingstandards.cpp.misra
18+
import codingstandards.cpp.rules.readofuninitializedmemory.ReadOfUninitializedMemory
19+
20+
class ValueOfAnObjectMustNotBeReadBeforeItHasBeenSetQuery extends ReadOfUninitializedMemorySharedQuery {
21+
ValueOfAnObjectMustNotBeReadBeforeItHasBeenSetQuery() {
22+
this = LifetimePackage::valueOfAnObjectMustNotBeReadBeforeItHasBeenSetQuery()
23+
}
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/readofuninitializedmemory/ReadOfUninitializedMemory.ql

rule_packages/cpp/Lifetime.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"MISRA-C++-2023": {
3+
"RULE-11-6-2": {
4+
"properties": {
5+
"enforcement": "undecidable",
6+
"obligation": "mandatory"
7+
},
8+
"queries": [
9+
{
10+
"description": "Reading from uninitialized indeterminate values may produce undefined behavior.",
11+
"kind": "problem",
12+
"name": "The value of an object must not be read before it has been set",
13+
"precision": "medium",
14+
"severity": "error",
15+
"short_name": "ValueOfAnObjectMustNotBeReadBeforeItHasBeenSet",
16+
"shared_implementation_short_name": "ReadOfUninitializedMemory",
17+
"tags": [
18+
"correctness",
19+
"security",
20+
"scope/system"
21+
]
22+
}
23+
],
24+
"title": "The value of an object must not be read before it has been set"
25+
},
26+
"RULE-6-8-3": {
27+
"properties": {
28+
"enforcement": "decidable",
29+
"obligation": "required"
30+
},
31+
"queries": [
32+
{
33+
"description": "An assignment operator shall not assign the address of an object with automatic storage duration to an object with a greater lifetime",
34+
"kind": "problem",
35+
"name": "An assignment operator shall not assign the address of an object with automatic storage duration to",
36+
"precision": "very-high",
37+
"severity": "error",
38+
"short_name": "AssignmentOperatorAssignTheAddressOfAnObjectWithAutomaticStorageDurationToAnObjectWithAGreaterLifetime",
39+
"tags": [
40+
"scope/single-translation-unit"
41+
]
42+
}
43+
],
44+
"title": "An assignment operator shall not assign the address of an object with automatic storage duration to an object with a greater lifetime"
45+
}
46+
}
47+
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ cpp,MISRA-C++-2023,RULE-10-4-1,Yes,Required,Decidable,Single Translation Unit,Th
925925
cpp,MISRA-C++-2023,RULE-11-3-1,Yes,Advisory,Decidable,Single Translation Unit,Variables of array type should not be declared,,Declarations2,Easy,
926926
cpp,MISRA-C++-2023,RULE-11-3-2,Yes,Advisory,Decidable,Single Translation Unit,The declaration of an object should contain no more than two levels of pointer indirection,A5-0-3,ImportMisra23,Import,
927927
cpp,MISRA-C++-2023,RULE-11-6-1,Yes,Advisory,Decidable,Single Translation Unit,All variables should be initialized,,Declarations2,Easy,
928-
cpp,MISRA-C++-2023,RULE-11-6-2,Yes,Mandatory,Undecidable,System,The value of an object must not be read before it has been set,A8-5-0,Lifetime,Very Hard,
928+
cpp,MISRA-C++-2023,RULE-11-6-2,Yes,Mandatory,Undecidable,System,The value of an object must not be read before it has been set,A8-5-0,Lifetime,Import
929929
cpp,MISRA-C++-2023,RULE-11-6-3,Yes,Required,Decidable,Single Translation Unit,"Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique",RULE-8-12,ImportMisra23,Import,
930930
cpp,MISRA-C++-2023,RULE-12-2-1,Yes,Advisory,Decidable,Single Translation Unit,Bit-fields should not be declared,A9-6-2,Banned,Easy,
931931
cpp,MISRA-C++-2023,RULE-12-2-2,Yes,Required,Decidable,Single Translation Unit,A bit-field shall have an appropriate type,RULE-6-1,ImportMisra23,Import,

0 commit comments

Comments
 (0)