Skip to content

Commit 654700f

Browse files
committed
Rule 14.3: Permit infinite loops using literals
1 parent 944f727 commit 654700f

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

c/misra/src/rules/RULE-14-3/ControllingExprInvariant.ql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ where
3939
(
4040
conditionAlwaysFalse(expr)
4141
or
42-
conditionAlwaysTrue(expr)
42+
conditionAlwaysTrue(expr) and
43+
// Exception allows for infinite loops, but we only permit that for literals like `true`
44+
not expr instanceof Literal
4345
)
4446
) and
4547
message = "Controlling expression in loop statement has invariant value."
@@ -53,6 +55,7 @@ where
5355
) and
5456
message = "Controlling expression in switch statement has invariant value."
5557
) and
56-
// Exclude macros, which may generate seemingly invariant expressions
58+
// Exclude cases where the controlling expressions is affected by a macro, because they can appear
59+
// invariant in a particular invocation, but be variant between invocations.
5760
not expr.isAffectedByMacro()
5861
select expr, message
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
| test.c:2:7:2:11 | ... > ... | Controlling expression in if statement has invariant value. |
2-
| test.c:13:10:13:16 | ... > ... | Controlling expression in loop statement has invariant value. |
3-
| test.c:14:9:14:13 | ... > ... | Controlling expression in if statement has invariant value. |
4-
| test.c:18:20:18:24 | ... < ... | Controlling expression in loop statement has invariant value. |
1+
| test.c:4:7:4:11 | ... > ... | Controlling expression in if statement has invariant value. |
2+
| test.c:15:10:15:16 | ... > ... | Controlling expression in loop statement has invariant value. |
3+
| test.c:16:9:16:13 | ... > ... | Controlling expression in if statement has invariant value. |
4+
| test.c:20:20:20:24 | ... < ... | Controlling expression in loop statement has invariant value. |
5+
| test.c:27:10:27:14 | ... < ... | Controlling expression in loop statement has invariant value. |

c/misra/test/rules/RULE-14-3/test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <stdbool.h>
2+
13
void f1(int p1) {
24
if (2 > 3) { // NON_COMPLIANT
35
}
@@ -18,3 +20,10 @@ void f2() {
1820
for (int i = 10; i < 5; i++) { // NON_COMPLIANT
1921
}
2022
}
23+
24+
void f3() {
25+
while (true) { // Permitted by exception
26+
}
27+
while (1 < 2) { // NON_COMPLIANT - likely an indication of a bug
28+
}
29+
}

0 commit comments

Comments
 (0)