Skip to content

M6-5-5: Consider const when determining whether an escaped reference modifies #380

@lcartey

Description

@lcartey

Affected rules

  • M6-5-5

Description

M6-5-5 identifies cases where a loop control variable is modified in the condition of a loop. It identifies two kinds of modifications: direct modification, and indirect modification - where a reference to the loop counter variable escapes the local scope, and could be modified. We currently assume that any reference that escapes the scope could be modified, but we should factor in whether the escaping function is const/non-const.

Specifically, we need to use isAddressOfAccessNonConst() instead of isAddressOfAccess() here:

loopControlVariableAccess.isAddressOfAccess()

Example

void loop(std::vector v) {
  std::vector<int>::iterator first = v.begin();
  std::vector<int>::iterator last = v.end();
  for (; first != last; first++) { // call to operator!= passes a reference to first
  }
}

Metadata

Metadata

Assignees

Labels

Difficulty-LowA false positive or false negative report which is expected to take <1 day effort to addressImpact-Highfalse positive/false negativeAn issue related to observed false positives or false negatives.user-reportIssue reported by an end user of CodeQL Coding Standards

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions