Skip to content

Commit

Permalink
Merge branch 'main' into michaelrfairhurst/implement-package-invalid-…
Browse files Browse the repository at this point in the history
…memory3
  • Loading branch information
lcartey authored Nov 22, 2024
2 parents 7014931 + 3667155 commit ef76126
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
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:93:15:93: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
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()
)
}

Expand All @@ -219,7 +219,7 @@ predicate isLoopCounterModifiedInStatement(
loopCounterAccess = loopCounter.getAnAccess() and
(
loopCounterAccess.isModified() or
loopCounterAccess.isAddressOfAccess()
loopCounterAccess.isAddressOfAccessNonConst()
) and
forLoop.getStmt().getChildStmt*() = loopCounterAccess.getEnclosingStmt()
}
Expand Down

0 comments on commit ef76126

Please sign in to comment.