github / codeql-coding-standards

This repository contains CodeQL queries and libraries which support various Coding Standards.
MIT License
129 stars 59 forks source link

`M6-5-3`: Triggered when loop counter is passed as const ref #755

Closed nbusser-sr closed 2 days ago

nbusser-sr commented 1 month ago

Affected rules

Rule 6–5–3 (Required) The loop-counter shall not be modified within condition

or statement.

Description

When passing a loop counter to a function as const ref, M6-5-3 is triggered.

NB: It is also triggered when passed as a mutable ref, but the function never uses the ref. This is less problematic since you should not give a mutable ref argument which is never modified.

Example

std::size_t const_ref(const std::size_t& iRef) {
    return iRef;
}
std::size_t ref(const std::size_t& iRef) {
    return iRef;
}
std::size_t copy(const std::size_t iRef) {
    return iRef;
}

for (std::size_t i{0}; i < 5; ++i) {
  const_ref(i); // Triggers M6-5-3
  ref(i);  // Triggers M6-5-3
  copy(i); // Ok
}
lcartey commented 1 month ago

Thanks! This should be relatively straight-forward to fix.

Note for future implementor, we need to modify Loops.qll to change:

loopCounterAccess.isAddressOfAccess()

to

loopCounterAccess.isAddressOfAccessNonConst()
fjatWbyT commented 2 days ago

I think this one is fixed now.