Skip to content

Commit 575be09

Browse files
committed
Fix issue with detecting ternary expressions.
1 parent 7155952 commit 575be09

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ where
3434
// ?: operator
3535
exists(ConditionalExpr parent |
3636
(
37-
parent.getThen().getAChild*() = zero and parent.getElse().getType() instanceof PointerType
37+
parent.getThen() = zero and parent.getElse().getType() instanceof PointerType
3838
or
39-
parent.getElse().getAChild*() = zero and parent.getThen().getType() instanceof PointerType
39+
parent.getElse() = zero and parent.getThen().getType() instanceof PointerType
4040
) and
4141
// exclude a common conditional pattern used in macros such as 'assert'
4242
not parent.isInMacroExpansion() and

c/misra/test/rules/RULE-11-9/MacroNullNotUsedAsIntegerNullPointerConstant.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
| test.c:17:8:17:8 | 0 | $@ uses zero-value integer constant expression as null pointer constant. | test.c:17:3:17:8 | ... = ... | Assignment to pointer |
33
| test.c:23:13:23:13 | 0 | $@ uses zero-value integer constant expression as null pointer constant. | test.c:23:3:23:13 | ... ? ... : ... | Ternary operator |
44
| test.c:24:8:24:8 | 0 | $@ uses zero-value integer constant expression as null pointer constant. | test.c:24:3:24:13 | ... ? ... : ... | Ternary operator |
5+
| test.c:31:14:31:14 | 0 | $@ uses zero-value integer constant expression as null pointer constant. | test.c:31:9:31:14 | ... = ... | Assignment to pointer |

c/misra/test/rules/RULE-11-9/test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@ void *f1(void *p1, int p2) {
2626
p2 ? p1 : (void*) 0; // COMPLIANT
2727
p2 ? p2 : 0; // COMPLIANT - p2 is not a pointer type
2828
p2 ? 0 : p2; // COMPLIANT - p2 is not a pointer type
29+
int x;
30+
int *y;
31+
p2 ? (p1 = 0) : p1; // NON_COMPLIANT - p1 is a pointer type
32+
p2 ? (p2 = 0) : p1; // COMPLIANT - p2 is not a pointer type
2933
return 0; // COMPLIANT
3034
}

change_notes/2023-07-28-rule-11-4-improvements.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
- `RULE-11-5` - `ConversionFromPointerToVoidIntoPointerToObject.ql`:
88
- Fixed issue #331 - consider `0` a null pointer constant.
99
- `RULE-11-6` - `CastBetweenPointerToVoidAndArithmeticType.ql`:
10-
- Fixed issue #331 - accept integer constant expressions with value `0` instead of null pointer constants.
10+
- Fixed issue #331 - accept integer constant expressions with value `0` instead of null pointer constants.
11+
- `RULE-11-9` - `MacroNullNotUsedAsIntegerNullPointerConstant.ql`:
12+
- Remove false positives in branches of ternary expressions, where `0` was used correctly.

0 commit comments

Comments
 (0)