Skip to content

Commit 0ec5f72

Browse files
committed
RULE-11-9: do not accept 0 as a null pointer constant
Rule 11.9 has a different set of requirements for null pointer constants to the other rules.
1 parent 2672d08 commit 0ec5f72

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

c/misra/src/rules/RULE-11-9/MacroNullNotUsedAsIntegerNullPointerConstant.ql

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,18 @@ import codingstandards.cpp.Type
1818
from Zero zero, Expr e, string type
1919
where
2020
not isExcluded(zero, Pointers1Package::macroNullNotUsedAsIntegerNullPointerConstantQuery()) and
21-
// exclude the base-case (NULL macros and void pointer casts)
22-
not isNullPointerConstant(zero) and
21+
// Exclude the base-case (NULL macros and void pointer casts)
22+
// Note: we cannot use the isNullPointerConstant predicate here because it permits
23+
// the use of `0` without casting, which is prohibited here.
24+
not (
25+
zero.findRootCause() instanceof NullMacro
26+
or
27+
// integer constant `0` explicitly cast to void pointer
28+
exists(Conversion c | c = zero.getConversion() |
29+
not c.isImplicit() and
30+
c.getUnderlyingType() instanceof VoidPointerType
31+
)
32+
) and
2333
(
2434
// ?: operator
2535
exists(ConditionalExpr parent |

cpp/common/src/codingstandards/cpp/Pointers.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class ArrayPointerArithmeticExpr extends PointerArithmeticExpr, ArrayExpr {
6262
predicate isNullPointerConstant(Expr e) {
6363
e.findRootCause() instanceof NullMacro
6464
or
65+
// 8.11 Pointer type conversions states:
66+
// A null pointer constant, i.e. the value 0, optionally cast to void *.
6567
e instanceof Zero
6668
or
6769
isNullPointerConstant(e.(Conversion).getExpr())

0 commit comments

Comments
 (0)