Skip to content

Commit 235eede

Browse files
committed
Rule 14.3: Handle do..while exception
1 parent cfed161 commit 235eede

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515

1616
import cpp
1717
import codingstandards.c.misra
18+
import codingstandards.c.misra.EssentialTypes
1819

19-
from ControlFlowNode expr, string message
20+
from Expr expr, string message
2021
where
2122
not isExcluded(expr, Statements5Package::controllingExprInvariantQuery()) and
2223
(
@@ -35,7 +36,11 @@ where
3536
exists(Loop loop |
3637
loop.getControllingExpr() = expr and
3738
(
38-
conditionAlwaysFalse(expr)
39+
conditionAlwaysFalse(expr) and
40+
not (
41+
getEssentialTypeCategory(getEssentialType(expr)) instanceof EssentiallyBooleanType and
42+
expr.getValue() = "0"
43+
)
3944
or
4045
conditionAlwaysTrue(expr) and
4146
// Exception allows for infinite loops, but we only permit that for literals like `true`

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ void f2() {
2222
}
2323

2424
void f3() {
25-
while (true) { // Permitted by exception
25+
while (true) { // COMPLIANT - permitted by exception 1
2626
}
2727
while (1 < 2) { // NON_COMPLIANT - likely an indication of a bug
2828
}
29+
}
30+
31+
void f4() {
32+
do {
33+
} while (0u == 1u); // COMPLIANT - by exception 2
2934
}

0 commit comments

Comments
 (0)