Skip to content

Commit c55a173

Browse files
First round of feedback
1 parent bfa67c1 commit c55a173

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

change_notes/2025-1-04-misra-c-technical-corrigenda-2.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- `RULE-8-3` - `DeclarationsOfAFunctionSameNameAndType.ql`:
22
- Implement new exception, unnamed parameters are not covered by this rule.
3-
- `RULE-10-2` - `AdditionSubtractionOnEssentiallCharType.ql`:
3+
- `RULE-10-2` - `AdditionSubtractionOnEssentiallyCharType.ql`:
44
- Disallow `+` and `-` operations with an essentially char type and other types larger than int type.
55
- Note, this change affects the essential type of such expressions, which may affect other essential types rules.
66
- `RULE-18-1`, `M5-0-16` - `PointerAndDerivedPointerMustAddressSameArray.ql`, `PointerAndDerivedPointerAccessDifferentArray.ql`:

cpp/common/src/codingstandards/cpp/rules/donotusepointerarithmetictoaddressdifferentarrays/DoNotUsePointerArithmeticToAddressDifferentArrays.qll

+16-7
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,28 @@ class CastedToBytePointer extends ArrayLikeAccess, Conversion {
104104
}
105105
}
106106

107+
predicate pointerRecastBarrier(DataFlow::Node barrier) {
108+
// Casting to a differently sized pointer
109+
exists(CStyleCast cast, Expr casted |
110+
cast.getExpr() = casted and casted = barrier.asConvertedExpr()
111+
|
112+
not casted.getType().(PointerType).getBaseType().getSize() =
113+
cast.getType().(PointerType).getBaseType().getSize()
114+
)
115+
}
116+
107117
/**
108118
* A data-flow configuration that tracks access to an array to type to an array index expression.
109119
* This is used to determine possible pointer to array creations.
110120
*/
111121
module ByteArrayToArrayExprConfig implements DataFlow::ConfigSig {
112122
predicate isSource(DataFlow::Node source) { exists(CastedToBytePointer a | a.getNode() = source) }
113123

114-
// TODO: casting to different size pointed-to-type invalidates
124+
predicate isBarrier(DataFlow::Node barrier) {
125+
// Casting to a differently sized pointer invalidates this analysis.
126+
pointerRecastBarrier(barrier)
127+
}
128+
115129
predicate isSink(DataFlow::Node sink) { exists(ArrayExpr c | c.getArrayBase() = sink.asExpr()) }
116130
}
117131

@@ -126,12 +140,7 @@ module ArrayToArrayExprConfig implements DataFlow::ConfigSig {
126140

127141
predicate isBarrier(DataFlow::Node barrier) {
128142
// Casting to a differently sized pointer invalidates this analysis.
129-
exists(CStyleCast cast, Expr casted |
130-
cast.getExpr() = casted and casted = barrier.asConvertedExpr()
131-
|
132-
not casted.getType().(PointerType).getBaseType().getSize() =
133-
cast.getType().(PointerType).getBaseType().getSize()
134-
)
143+
pointerRecastBarrier(barrier)
135144
}
136145

137146
predicate isSink(DataFlow::Node sink) { exists(ArrayExpr c | c.getArrayBase() = sink.asExpr()) }

cpp/common/test/rules/donotusepointerarithmetictoaddressdifferentarrays/test.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ void f1() {
4040
void *p22 = &p21[0]; // COMPLIANT
4141
void *p23 = &p21[100]; // NON_COMPLIANT[FALSE_NEGATIVE]
4242

43+
// Casting a byte pointer to a differently sized type that isn't char
44+
// invalidates analysis
45+
long *p24 = (long *)p15;
46+
void *p25 = &p24[0]; // COMPLIANT
47+
void *p26 = &p24[100]; // NON_COMPLIANT[FALSE_NEGATIVE]
48+
4349
// Void pointers have size zero and can't be analyzed.
44-
void *p24 = 0;
45-
unsigned char *p25 = (unsigned char *)p24;
46-
void *p26 = &p25[100]; // COMPLIANT
50+
void *p27 = 0;
51+
unsigned char *p28 = (unsigned char *)p27;
52+
void *p29 = &p28[100]; // COMPLIANT
4753
}

rule_packages/c/Statements5.json

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
]
2121
}
2222
],
23+
"implementation_scope": {
24+
"description": "Not all invariant logical expressions which contain dynamic values are detected to be invariant, for instance, `x < 3 && x > 5` where x does not have a statically known value."
25+
},
2326
"title": "Controlling expressions shall not be invariant"
2427
},
2528
"RULE-15-5": {

0 commit comments

Comments
 (0)