Skip to content

Commit 3667155

Browse files
authoredNov 21, 2024··
Merge pull request #773 from fjatWbyT/fix-issue-755
Fix issue 755 and typo in A18-1-1 test
2 parents 9dc1ca4 + 50fe082 commit 3667155

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed
 
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M6-5-3` - `Loops.qll`:
2+
- Fixes #755. Specifies that the access to the loop counter must be via non-const address.

‎cpp/autosar/test/rules/A18-1-1/test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ int test_c_arrays() {
1111
int x[100]; // NON_COMPLIANT
1212
constexpr int a[]{0, 1, 2}; // NON_COMPLIANT
1313

14-
__func__; // COMPLAINT
14+
__func__; // COMPLIANT
1515
return 0;
16-
}
16+
}

‎cpp/autosar/test/rules/M6-5-3/LoopCounterModifiedWithinStatement.expected

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
| test.cpp:25:35:25:35 | x | Loop counters should not be modified within a statement in a for loop. |
33
| test.cpp:36:5:36:5 | x | Loop counters should not be modified within a statement in a for loop. |
44
| test.cpp:43:9:43:9 | i | Loop counters should not be modified within a statement in a for loop. |
5+
| test.cpp:93:15:93:15 | i | Loop counters should not be modified within a statement in a for loop. |

‎cpp/autosar/test/rules/M6-5-3/test.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,54 @@ void test_loop_counter_mod_in_side_effect() {
4343
inc(i); // NON_COMPLIANT - modifies `i`
4444
}
4545
}
46+
47+
void test_loop_counter_reference_mod_in_condition() {
48+
auto loop = [](int &i) {
49+
for (; (i++ < 10); i++) { // NON_COMPLIANT
50+
}
51+
};
52+
int i = 0;
53+
loop(i);
54+
}
55+
56+
void test_loop_counter_reference_mod() {
57+
auto loop = [](int &i) {
58+
for (; i < 10; i++) { // COMPLIANT
59+
}
60+
};
61+
int i = 0;
62+
loop(i);
63+
}
64+
65+
void test_loop_const_reference() {
66+
auto loop = []([[maybe_unused]] int const &i) {
67+
for (int i = 0; i < 10; i++) { // COMPLIANT
68+
}
69+
};
70+
int i = 0;
71+
loop(i);
72+
}
73+
74+
void test_loop_counter_reference_mod_in_statement() {
75+
auto loop = [](int &i) {
76+
for (; (i < 10); i++) {
77+
i++; // NON_COMPLIANT
78+
}
79+
};
80+
int i = 0;
81+
loop(i);
82+
}
83+
84+
int const_reference(int const &i) { return i; }
85+
86+
int reference(int &i) { return i; }
87+
88+
int copy(int i) { return i; }
89+
90+
void test_pass_argument_by() {
91+
for (int i = 0; i < 10; i++) {
92+
const_reference(i); // COMPLIANT
93+
reference(i); // NON_COMPLIANT
94+
copy(i); // COMPLIANT
95+
}
96+
}

‎cpp/common/src/codingstandards/cpp/Loops.qll

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ predicate isLoopCounterModifiedInCondition(ForStmt forLoop, VariableAccess loopC
204204
loopCounterAccess = getAnIterationVariable(forLoop).getAnAccess() and
205205
(
206206
loopCounterAccess.isModified() or
207-
loopCounterAccess.isAddressOfAccess()
207+
loopCounterAccess.isAddressOfAccessNonConst()
208208
)
209209
}
210210

@@ -219,7 +219,7 @@ predicate isLoopCounterModifiedInStatement(
219219
loopCounterAccess = loopCounter.getAnAccess() and
220220
(
221221
loopCounterAccess.isModified() or
222-
loopCounterAccess.isAddressOfAccess()
222+
loopCounterAccess.isAddressOfAccessNonConst()
223223
) and
224224
forLoop.getStmt().getChildStmt*() = loopCounterAccess.getEnclosingStmt()
225225
}

0 commit comments

Comments
 (0)
Please sign in to comment.