Rules / buffer-window-overrun
Modbus slave buffer window shifted but its size left unchanged
The exchange block gets a shifted start (ADR(a) + 2) but the old size, SIZEOF(a). The window runs past the end of the array by exactly the shift.
Severity: Warning
Rule id: buffer-window-overrun
Buffer window shifted but size unchanged
What the rule checks
An exchange block (Modbus slave, serial port, file write) is given the start of a memory area and its size. If the start is shifted (ADR(a) + 2 or ADR(a[k]) with k above the lower bound) while the size stays the full SIZEOF(a), the window runs past the end of the array by exactly the shift. Reading the last registers returns a neighbouring variable's contents, and a master write (Modbus functions 6/16) corrupts it; which variable depends on how the linker laid out memory. The compiler does not catch it, as both arguments are type-correct. The mistake usually appears when the first register is shifted to match the HMI map and the size is forgotten.
What it does to the plant
The master reads a neighbouring variable, and writes with functions 6 and 16 corrupt it; where exactly depends on the memory layout.
Example
VAR_GLOBAL aHmiRegs : ARRAY[0..99] OF WORD;END_VARPROGRAM LinkInitVAR fbSlave : MB_SLAVE_FB;END_VARfbSlave(Port := 1, DevAddr := 1, pBuffer := ADR(gvl.aHmiRegs) + 2, BufSize := SIZEOF(gvl.aHmiRegs));
VAR_GLOBAL aHmiRegs : ARRAY[0..99] OF WORD;END_VARPROGRAM LinkInitVAR fbSlave : MB_SLAVE_FB;END_VARfbSlave(Port := 1, DevAddr := 1, pBuffer := ADR(gvl.aHmiRegs) + 2, BufSize := SIZEOF(gvl.aHmiRegs) - 2);
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
- Reduce the size by the shift:
BufSize := SIZEOF(a) - 2(shift in bytes), or declare the array to match the window. - Check the HMI or master register map: the last register it accesses must lie inside the array.
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