Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 755 and typo in A18-1-1 test #773

Merged
merged 7 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2024-10-22-fix-fp-m6-5-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `M6-5-3` - `Loops.qll`:
- Fixes #755. Specifies that the access to the loop counter must be via non-const address.
4 changes: 2 additions & 2 deletions cpp/autosar/test/rules/A18-1-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ int test_c_arrays() {
int x[100]; // NON_COMPLIANT
constexpr int a[]{0, 1, 2}; // NON_COMPLIANT

__func__; // COMPLAINT
__func__; // COMPLIANT
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
| test.cpp:25:35:25:35 | x | Loop counters should not be modified within a statement in a for loop. |
| test.cpp:36:5:36:5 | x | Loop counters should not be modified within a statement in a for loop. |
| test.cpp:43:9:43:9 | i | Loop counters should not be modified within a statement in a for loop. |
| test.cpp:99:15:99:15 | i | Loop counters should not be modified within a statement in a for loop. |
51 changes: 51 additions & 0 deletions cpp/autosar/test/rules/M6-5-3/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,54 @@ void test_loop_counter_mod_in_side_effect() {
inc(i); // NON_COMPLIANT - modifies `i`
}
}

void test_loop_counter_reference_mod_in_condition() {
auto loop = [](int &i) {
for (; (i++ < 10); i++) { // NON_COMPLIANT
}
};
int i = 0;
loop(i);
}

void test_loop_counter_reference_mod() {
auto loop = [](int &i) {
for (; i < 10; i++) { // COMPLIANT
}
};
int i = 0;
loop(i);
}

void test_loop_const_reference() {
auto loop = []([[maybe_unused]] int const &i) {
for (int i = 0; i < 10; i++) { // COMPLIANT
}
};
int i = 0;
loop(i);
}

void test_loop_counter_reference_mod_in_statement() {
auto loop = [](int &i) {
for (; (i < 10); i++) {
i++; // NON_COMPLIANT
}
};
int i = 0;
loop(i);
}

int const_reference(int const &i) { return i; }

int reference(int &i) { return i; }

int copy(int i) { return i; }

void test_pass_argument_by() {
for (int i = 0; i < 10; i++) {
const_reference(i); // COMPLIANT
reference(i); // NON_COMPLIANT
lcartey marked this conversation as resolved.
Show resolved Hide resolved
copy(i); // COMPLIANT
}
}
4 changes: 2 additions & 2 deletions cpp/common/src/codingstandards/cpp/Loops.qll
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ predicate isLoopCounterModifiedInCondition(ForStmt forLoop, VariableAccess loopC
loopCounterAccess = getAnIterationVariable(forLoop).getAnAccess() and
(
loopCounterAccess.isModified() or
loopCounterAccess.isAddressOfAccess()
loopCounterAccess.isAddressOfAccessNonConst()
lcartey marked this conversation as resolved.
Show resolved Hide resolved
)
}

Expand All @@ -219,7 +219,7 @@ predicate isLoopCounterModifiedInStatement(
loopCounterAccess = loopCounter.getAnAccess() and
(
loopCounterAccess.isModified() or
loopCounterAccess.isAddressOfAccess()
loopCounterAccess.isAddressOfAccessNonConst()
lcartey marked this conversation as resolved.
Show resolved Hide resolved
) and
forLoop.getStmt().getChildStmt*() = loopCounterAccess.getEnclosingStmt()
}
Expand Down
Loading