Contents

Division by zero

Detects division where the divisor is zero.

Overview

Use this check to detect integer and float division where the divisor is zero. Division by zero has undefined behavior, and can result in crashes or incorrect program output. Available in Xcode 9 and later.

Integer division by zero in C

In the following code, the for loop performs division by zero on its first iteration:

int sum = 10;
for (int i = 0; i < 64; ++i) {
    sum /= i; // Error: division by zero on the first iteration
}

Solution

Modify the logic to check for and avoid division when the divisor might equal zero.

See Also

Undefined Behavior Sanitizer