Skip to content

Commit f9a6844

Browse files
committed
Rule 8.2.6: Query improvements
- Simplify the query implementation - Improve query message
1 parent 251bf1a commit f9a6844

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

cpp/misra/src/rules/RULE-8-2-6/IntToPointerCastProhibited.ql

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,26 @@
1515
import cpp
1616
import codingstandards.cpp.misra
1717

18-
from Cast cast, Type sourceType, Type targetType
18+
from Cast cast, Type sourceType, PointerType targetType, string typeKind
1919
where
2020
not isExcluded(cast, Conversions2Package::intToPointerCastProhibitedQuery()) and
21-
sourceType = cast.getExpr().getType().stripTopLevelSpecifiers() and
22-
targetType = cast.getType().stripTopLevelSpecifiers() and
23-
targetType instanceof PointerType and
24-
not targetType instanceof FunctionPointerType and
25-
not (
26-
// Exception: casts between void pointers are allowed
27-
targetType.(PointerType).getBaseType().stripTopLevelSpecifiers() instanceof VoidType and
28-
sourceType instanceof PointerType and
29-
sourceType.(PointerType).getBaseType().stripTopLevelSpecifiers() instanceof VoidType
30-
) and
21+
sourceType = cast.getExpr().getType().getUnspecifiedType() and
22+
targetType = cast.getType().getUnspecifiedType() and
3123
(
3224
// Integral types
33-
sourceType instanceof IntegralType
25+
sourceType instanceof IntegralType and
26+
typeKind = "integral"
3427
or
3528
// Enumerated types
36-
sourceType instanceof Enum
29+
sourceType instanceof Enum and
30+
typeKind = "enumerated"
3731
or
3832
// Pointer to void type
39-
sourceType instanceof PointerType and
40-
sourceType.(PointerType).getBaseType().stripTopLevelSpecifiers() instanceof VoidType
33+
sourceType.(PointerType).getBaseType() instanceof VoidType and
34+
typeKind = "pointer to void" and
35+
// Exception: casts between void pointers are allowed
36+
not targetType.getBaseType() instanceof VoidType
4137
)
4238
select cast,
43-
"Cast from '" + sourceType.toString() + "' to '" + targetType.toString() + "' is prohibited."
39+
"Cast from " + typeKind + " type '" + cast.getExpr().getType() + "' to pointer type '" +
40+
cast.getType() + "'."
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
| test.cpp:15:20:15:53 | reinterpret_cast<TestStruct *>... | Cast from 'signed int' to 'TestStruct *' is prohibited. |
2-
| test.cpp:16:20:16:53 | reinterpret_cast<TestStruct *>... | Cast from 'signed long' to 'TestStruct *' is prohibited. |
3-
| test.cpp:17:22:17:57 | reinterpret_cast<int32_t *>... | Cast from 'signed long' to 'int32_t *' is prohibited. |
4-
| test.cpp:24:20:24:53 | reinterpret_cast<TestStruct *>... | Cast from 'TestEnum' to 'TestStruct *' is prohibited. |
5-
| test.cpp:25:22:25:57 | reinterpret_cast<int32_t *>... | Cast from 'TestEnum' to 'int32_t *' is prohibited. |
6-
| test.cpp:32:20:32:48 | static_cast<TestStruct *>... | Cast from 'void *' to 'TestStruct *' is prohibited. |
7-
| test.cpp:33:20:33:53 | reinterpret_cast<TestStruct *>... | Cast from 'void *' to 'TestStruct *' is prohibited. |
8-
| test.cpp:34:22:34:52 | static_cast<int32_t *>... | Cast from 'void *' to 'int32_t *' is prohibited. |
9-
| test.cpp:35:22:35:57 | reinterpret_cast<int32_t *>... | Cast from 'void *' to 'int32_t *' is prohibited. |
10-
| test.cpp:43:14:43:41 | reinterpret_cast<void *>... | Cast from 'signed int' to 'void *' is prohibited. |
11-
| test.cpp:44:14:44:41 | reinterpret_cast<void *>... | Cast from 'signed long' to 'void *' is prohibited. |
1+
| test.cpp:15:20:15:53 | reinterpret_cast<TestStruct *>... | Cast from integral type 'int32_t' to pointer type 'TestStruct *'. |
2+
| test.cpp:16:20:16:53 | reinterpret_cast<TestStruct *>... | Cast from integral type 'int64_t' to pointer type 'TestStruct *'. |
3+
| test.cpp:17:22:17:57 | reinterpret_cast<int32_t *>... | Cast from integral type 'int64_t' to pointer type 'int32_t *'. |
4+
| test.cpp:24:20:24:53 | reinterpret_cast<TestStruct *>... | Cast from enumerated type 'TestEnum' to pointer type 'TestStruct *'. |
5+
| test.cpp:25:22:25:57 | reinterpret_cast<int32_t *>... | Cast from enumerated type 'TestEnum' to pointer type 'int32_t *'. |
6+
| test.cpp:32:20:32:48 | static_cast<TestStruct *>... | Cast from pointer to void type 'void *' to pointer type 'TestStruct *'. |
7+
| test.cpp:33:20:33:53 | reinterpret_cast<TestStruct *>... | Cast from pointer to void type 'void *' to pointer type 'TestStruct *'. |
8+
| test.cpp:34:22:34:52 | static_cast<int32_t *>... | Cast from pointer to void type 'void *' to pointer type 'int32_t *'. |
9+
| test.cpp:35:22:35:57 | reinterpret_cast<int32_t *>... | Cast from pointer to void type 'void *' to pointer type 'int32_t *'. |
10+
| test.cpp:43:14:43:41 | reinterpret_cast<void *>... | Cast from integral type 'int32_t' to pointer type 'void *'. |
11+
| test.cpp:44:14:44:41 | reinterpret_cast<void *>... | Cast from integral type 'int64_t' to pointer type 'void *'. |

0 commit comments

Comments
 (0)