Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2026-10-27-fix-fp-rule-8-2-6-implicit-casts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `RULE-8-2-6` - `IntToPointerCastProhibited.ql`:
- Remove false positives for implicit conversions, such as the implicit `this` in unevaluated operands (e.g. `sizeof(member)`).
3 changes: 3 additions & 0 deletions cpp/misra/src/rules/RULE-8-2-6/IntToPointerCastProhibited.ql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import codingstandards.cpp.misra
from Cast cast, Type sourceType, PointerType targetType, string typeKind
where
not isExcluded(cast, Conversions2Package::intToPointerCastProhibitedQuery()) and
// Implicit conversions are not casts, e.g. null pointer constants or the implicit `this` in an
// unevaluated operand.
not cast.isImplicit() and
sourceType = cast.getExpr().getType().getUnspecifiedType() and
targetType = cast.getType().getUnspecifiedType() and
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
| test.cpp:35:22:35:57 | reinterpret_cast<int32_t *>... | Cast from pointer to void type 'void *' to pointer type 'int32_t *'. |
| test.cpp:43:14:43:41 | reinterpret_cast<void *>... | Cast from integral type 'int32_t' to pointer type 'void *'. |
| test.cpp:44:14:44:41 | reinterpret_cast<void *>... | Cast from integral type 'int64_t' to pointer type 'void *'. |
| test.cpp:100:12:100:58 | reinterpret_cast<TestStruct *>... | Cast from integral type 'unsigned long' to pointer type 'TestStruct *'. |
14 changes: 13 additions & 1 deletion cpp/misra/test/rules/RULE-8-2-6/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,16 @@ void test_compliant_regular_pointer_operations() {
// Regular pointer operations that don't involve forbidden casts
TestStruct *l4 = l2; // COMPLIANT
std::int32_t *l5 = l3; // COMPLIANT
}
}
// The implicit `this` of a member access in an unevaluated operand is
// extracted as a conversion of 0 to a pointer. It is not a cast.
struct ImplicitThis {
std::int32_t m1;
static constexpr unsigned long size() noexcept {
return sizeof(m1); // COMPLIANT
}
TestStruct *nonCompliant(std::int64_t l1) {
// The implicit `this` of m1 is compliant, the explicit cast is not
return reinterpret_cast<TestStruct *>(l1 + sizeof(m1)); // NON_COMPLIANT
}
};
Loading