CXX.CWARN.ITER.EXTERN

Externally declared loop iterator variables are forbidden.

Avoid externally created loop boundary variables.

Vulnerability and risk

This can lead to confusion if loop index variables are used for other loops as well and thus are modfied elsewhere or are not initialized as expected.

Mitigation and prevention

Consider creating a separate index variable specifically for use in this loop.

Example

Copy
int iGlobalIdx;
for (iGlobalIdx = 0; iGlobalIdx < array1.GetSize(); iGlobalIdx++)
{
  for(iGlobalIdx = 0; iGlobalIdx < array2.GetSize(); iGlobalIdx++)
  {
    someNumber = array2.GetAt(iGlobalIdx);
    someNumber++;
  }
  .
  .
  .
  if(array1.GetAt(iGlobalIdx) == 5)
  {
    ...
  }
}

In this example the use of iGlobalIdx for the nested loop will result in that value potentially being out of bounds when used upon array1 despite the guard in the outer loop.