Skip to content

Commit b0014ca

Browse files
committed
Use loop counter definition from CERT
1 parent b26983c commit b0014ca

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

c/cert/src/rules/FLP30-C/FloatingPointLoopCounters.ql

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,36 @@ import cpp
1616
import codingstandards.c.cert
1717
import codingstandards.cpp.Loops
1818

19-
from Loop loop
19+
/*
20+
* A variable that is increased or decreased by a fixed amount on each iteration.
21+
*/
22+
23+
class InductionVariable extends Variable {
24+
Loop loop;
25+
Expr update;
26+
27+
InductionVariable() {
28+
update.getParent+() = loop and
29+
(
30+
update.(AssignArithmeticOperation).getRValue().isConstant() and
31+
update.(AssignArithmeticOperation).getLValue() = this.getAnAccess()
32+
or
33+
exists(BinaryArithmeticOperation binop |
34+
update.(Assignment).getLValue() = this.getAnAccess() and
35+
update.(Assignment).getRValue() = binop and
36+
binop.getAnOperand() = this.getAnAccess() and
37+
binop.getAnOperand().isConstant()
38+
)
39+
or
40+
update.(CrementOperation).getOperand() = this.getAnAccess()
41+
)
42+
}
43+
}
44+
45+
from Loop loop, InductionVariable loopCounter, ComparisonOperation comparison
2046
where
2147
not isExcluded(loop, Statements4Package::floatingPointLoopCountersQuery()) and
22-
exists(WhileStmt while |
23-
while.getCondition().getType() instanceof FloatType and
24-
loop = while
25-
)
26-
or
27-
exists(ForStmt for, Variable counter |
28-
isForLoopWithFloatingPointCounters(for, counter) and for = loop
29-
)
30-
select loop, "Loop $@ has a floating-point type.", loop.getControllingExpr(), "counter"
48+
loop.getControllingExpr() = comparison and
49+
comparison.getAnOperand() = loopCounter.getAnAccess() and
50+
loopCounter.getType() instanceof FloatingPointType
51+
select loop, "Loop using a $@ of type floating-point.", loopCounter, "loop counter"
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
| test.c:3:3:4:3 | for(...;...;...) ... | Loop $@ has a floating-point type. | test.c:3:18:3:26 | ... < ... | counter |
2-
| test.c:5:3:6:3 | while (...) ... | Loop $@ has a floating-point type. | test.c:5:10:5:17 | ... - ... | counter |
1+
| test.c:3:3:4:3 | for(...;...;...) ... | Loop using a $@ of type floating-point. | test.c:2:9:2:9 | f | loop counter |
2+
| test.c:5:3:7:3 | while (...) ... | Loop using a $@ of type floating-point. | test.c:2:9:2:9 | f | loop counter |
3+
| test.c:9:3:11:22 | do (...) ... | Loop using a $@ of type floating-point. | test.c:2:9:2:9 | f | loop counter |

c/cert/test/rules/FLP30-C/test.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@ void f1() {
22
float f = 0.0F;
33
for (f = 0.0F; f < 10.0F; f += 0.2F) { // NON_COMPLIANT
44
}
5-
while (f - 0.0F) { // NON_COMPLIANT
5+
while (f < 10.0F) { // NON_COMPLIANT
6+
f = f * 2.0F;
67
}
8+
9+
do {
10+
f *= 2.0F;
11+
} while (f < 10.0F); // NON_COMPLIANT
712
}
813

914
void f2() {
1015

1116
for (int i = 0; i < 10; i++) { // COMPLIANT
1217
}
13-
while (4 - 4) { // COMPLIANT
18+
int j = 0;
19+
while (j < 10) { // COMPLIANT
20+
j = j * 2;
1421
}
22+
23+
do {
24+
j++;
25+
} while (j < 10); // COMPLIANT
1526
}

0 commit comments

Comments
 (0)