Skip to content

Commit ab77d68

Browse files
geoffw0Copilot
andcommitted
C++: Test floating-point loop counters
Cover float, double, long double, and typedef counters alongside integral loops for cpp/loop-variable-float; document unsupported compound updates. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ce02e4a commit ab77d68

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.cpp:5:14:5:14 | f | Floating point variables should not be used as loop counters. |
2+
| test.cpp:10:15:10:15 | d | Floating point variables should not be used as loop counters. |
3+
| test.cpp:15:15:15:16 | ld | Floating point variables should not be used as loop counters. |
4+
| test.cpp:42:13:42:13 | r | Floating point variables should not be used as loop counters. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: jsf/4.24 Control Flow Structures/AV Rule 197.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
void use(double);
2+
3+
void test()
4+
{
5+
for (float f = 0.0f; f < 1.0f; f = f + 0.1f) // $ Alert // BAD: float loop counter
6+
{
7+
use(f);
8+
}
9+
10+
for (double d = 0.0; d < 10.0; d++) // $ Alert // BAD: double loop counter
11+
{
12+
use(d);
13+
}
14+
15+
long double ld; // $ Alert // BAD: long double loop counter
16+
for (ld = 10.0; ld > 0.0; ld--)
17+
{
18+
use(ld);
19+
}
20+
21+
for (double c = 0.0; c < 1.0; c += 0.1) // BAD [NOT DETECTED]: compound assignment updates are not recognized
22+
{
23+
use(c);
24+
}
25+
26+
for (int i = 0; i < 10; i++) // GOOD: integer loop counter
27+
{
28+
use(i * 0.1);
29+
}
30+
31+
double x = 0.0; // GOOD: not a `for` loop counter
32+
while (x < 1.0)
33+
{
34+
x = x + 0.1;
35+
}
36+
}
37+
38+
typedef float real;
39+
40+
void test_typedef()
41+
{
42+
for (real r = 0.0f; r < 1.0f; r = r + 0.5f) // $ Alert // BAD: float loop counter via typedef
43+
{
44+
use(r);
45+
}
46+
}

0 commit comments

Comments
 (0)