Rules / div-by-zero
Division by zero in Structured Text: how to find it before site start-up
The rule looks for division or MOD by a literal zero and by a variable that can be zero without a guard. In CODESYS this raises a runtime exception unless a CheckDiv function is present.
Severity: Critical
Rule id: div-by-zero
Division by a literal zero
What the rule checks
Division or MOD by a literal zero is a guaranteed runtime exception (division by zero) whenever this line is reached.
What it does to the plant
The task stops on an exception the moment the divisor hits zero, typically at start-up, a counter reset or a sensor break.
Example
PROGRAM AverageVAR xReset : BOOL; iSum : INT; iCount : INT; iAvg : INT;END_VARIF xReset THEN iCount := 0;END_IFiAvg := iSum / iCount;
PROGRAM AverageVAR xReset : BOOL; iSum : INT; iCount : INT; iAvg : INT;END_VARIF xReset THEN iCount := 0;END_IFIF iCount <> 0 THEN iAvg := iSum / iCount;END_IF
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
- Remove the division by 0; it is probably a typo in the divisor.
- If the divisor can be zero dynamically, guard it:
IF d <> 0 THEN r := x / d; 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
In-depth article
2026-11-26