Skip to content

Commit 7155952

Browse files
committed
RULE-11-9: Improve ternary test cases
This rule intends to prohibit the use of 0 within branches of ternaries, where the ternary expression itself produces a pointer expression because the other branch of the ternary is a pointer. The previous test case didn't capture this behaviour, instead looking for assignments within branches.
1 parent 0ec5f72 commit 7155952

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
| test.c:15:13:15:13 | 0 | $@ uses zero-value integer constant expression as null pointer constant. | test.c:15:7:15:13 | ... == ... | Equality operator |
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 |
3-
| test.c:25:20:25:20 | 0 | $@ uses zero-value integer constant expression as null pointer constant. | test.c:25:3:25:35 | ... ? ... : ... | Ternary operator |
4-
| test.c:25:20:25:20 | 0 | $@ uses zero-value integer constant expression as null pointer constant. | test.c:25:15:25:20 | ... = ... | Assignment to pointer |
3+
| 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 |
4+
| 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 |

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ void *f1(void *p1, int p2) {
1919
p1 = NULL; // COMPLIANT
2020
if (p2 == 0) { // COMPLIANT
2121
return NULL;
22-
} // COMPLIANT
23-
(p1) ? (p1 = NULL) : (p1 = NULL); // COMPLIANT
24-
(p2 > 0) ? (p1 = NULL) : (p1 = NULL); // COMPLIANT
25-
(p2 > 0) ? (p1 = 0) : (p1 = NULL); // NON_COMPLIANT
22+
}
23+
p2 ? p1 : 0; // NON_COMPLIANT
24+
p2 ? 0 : p1; // NON_COMPLIANT
25+
p2 ? (void*) 0 : p1; // COMPLIANT
26+
p2 ? p1 : (void*) 0; // COMPLIANT
27+
p2 ? p2 : 0; // COMPLIANT - p2 is not a pointer type
28+
p2 ? 0 : p2; // COMPLIANT - p2 is not a pointer type
2629
return 0; // COMPLIANT
2730
}

0 commit comments

Comments
 (0)