Rules / array-bounds
Array index out of bounds in Structured Text: CODESYS does not always catch it
A constant index outside the declared ARRAY[lo..hi] reads or writes foreign memory every time the line runs. Without an active CheckBounds function the runtime will not intercept it.
Severity: Critical
Rule id: array-bounds
Array index out of bounds
What the rule checks
A constant index falls outside the declared ARRAY[lo..hi] bounds, so reaching this line reads or writes memory that isn't yours. Without active implicit checks the runtime won't intercept it. Checked for one-dimensional arrays with numeric bounds and a literal index.
What it does to the plant
Neighbouring variables get corrupted: random setpoints, commands that seem to come from nowhere, faults that never reproduce on the bench.
Example
PROGRAM ZonesVAR aTemp : ARRAY[1..4] OF REAL;END_VARaTemp[5] := 0.0;
PROGRAM ZonesVAR aTemp : ARRAY[1..4] OF REAL;END_VARaTemp[4] := 0.0;
Both listings are run through the linter by an automated test: the rule fires on the first and stays silent on the second.
How to fix
- Bring the index into the declared range or widen the array bounds.
- Check before access:
IF idx >= LO AND idx <= HI THEN arr[idx] … END_IF
Upload a PLCopen XML or CODESYS .export file: the report is free and the code is not stored.
The same linter without uploading a file: command line and GitHub Action.
Related rules
2026-11-26