free quality report for PLCopenXML exports

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

Bad: the rule fires
PROGRAM AverageVAR    xReset : BOOL;    iSum : INT;    iCount : INT;    iAvg : INT;END_VARIF xReset THEN    iCount := 0;END_IFiAvg := iSum / iCount;
Fixed: the rule is silent
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

  1. Remove the division by 0; it is probably a typo in the divisor.
  2. If the divisor can be zero dynamically, guard it: IF d <> 0 THEN r := x / d; END_IF
Check your project

Upload a PLCopen XML or CODESYS .export file: the report is free and the code is not stored.

Local version on GitHub →

The same linter without uploading a file: command line and GitHub Action.

Related rules

In-depth article

2026-11-26

Processing happens in memory, and the uploaded code is not stored. The tool advises; code changes are up to the engineer (see what the linter can't see). What happens to your code → · Rule reference · Your code & privacy · Industry benchmark · Feedback · GitHub