Detects the FOR statements where the index variable is used to access an array index and exceeds the range of the array index
Justification: Arrays are typically processed in FOR
loops. The start and end value of the counter variable should typically match (or at least not exceed) the upper and lower bounds of the array. A typical cause of error is detected here when array bounds are changed and constants are not carefully used; or when a different value is used accidentally in the FOR
loop than in the array declaration.
Importance: High
Example
PROGRAM PLC_PRG
VAR
iIndex1,iIndex2,iIndex3 : INT;
arWord : ARRAY[1..100] OF WORD;
arararINT : ARRAY[1..9,1..9,1..9] OF INT;
arUSINT : ARRAY[0..99] OF USINT;
END_VAR
//1 violation of the rule(lower range is exeeded): SA0080
FOR iIndex1 := INT#0 TO INT#100 DO
arWord[iIndex1] := INT_TO_WORD(iIndex1);
END_FOR
//6 violations (lower and upper range is exeeded for each array dimension): 3SA0080
FOR iIndex2 := INT#0 TO INT#10 DO
arararINT[iIndex2, iIndex2, iIndex2] := iIndex2;
END_FOR
//1 violation (upper range is exeeded by the end result of the index), hievor expressions on index are not evaluated -> OK
FOR iIndex3 := INT#0 TO INT#50 DO
arUSINT[iIndex3 * INT#2] := INT_TO_USINT(iIndex3);
END_FOR
--> SA0080: Schleifenindexbereich für Arrayindex 'Index1' überschreitet Arraybereich
--> SA0080: Schleifenindexbereich für Arrayindex 'Index2' überschreitet Arraybereich