检测用于访问数组索引的索引变量超出数组索引范围的 FOR 语句
理由数组通常在FOR
循环中处理。计数器变量的起始值和结束值通常应与数组的上下限相匹配(或至少不超过数组的上下限)。当数组边界发生变化而常量使用不慎,或者在FOR
循环中意外使用了与数组声明中不同的值时,就会检测到典型的错误原因。
重要性:高
示例
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 exceeded 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 exceeded by the end result of the index), previous 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: Loop index range of 'Index1' exceeds array range --> SA0080: Loop index range of 'Index2' exceeds array range