Divide By Zero
Description
Divide By Zero is a vulnerability that occurs when a product divides a value by zero, causing undefined behavior, crashes, or exceptions. This weakness typically occurs when an unexpected value is provided to the product, when input validation is insufficient, or when an error condition goes undetected. It frequently appears in calculations involving physical dimensions such as size, length, width, and height, as well as in time calculations, ratios, and averages. In most programming languages and processors, division by zero causes a runtime exception or hardware fault that terminates the program if not caught.
Risk
Division by zero can cause denial of service through application crashes. In server applications, a crash can interrupt service for all users. In embedded systems or critical applications, an unhandled divide-by-zero can cause system resets or undefined behavior. Some security-critical operations may be bypassed if the application crashes before completing them. While primarily an availability issue, division by zero can sometimes be chained with other vulnerabilities - for example, if the crash leaves data in an inconsistent state or if the exception handling reveals sensitive information.
Solution
Always validate that divisor values are not zero before performing division. Implement proper input validation for all values that may be used as divisors. Check for zero explicitly and handle it appropriately - either return an error, use a default value, or skip the calculation. Use safe math libraries that handle edge cases. Consider whether the zero value indicates an error condition that should be handled differently. In languages that throw exceptions, wrap division operations in try-catch blocks when the divisor may be zero.
Common Consequences
| Impact | Details |
|---|---|
| Availability | Scope: Availability A divide by zero results in a crash, causing the application to exit or restart unexpectedly, leading to denial of service. |
Example Code
Vulnerable Code
// Vulnerable: No check for zero divisor
int vulnerable_calculate_average(int *values, int count) {
int sum = 0;
for (int i = 0; i < count; i++) {
sum += values[i];
}
// Vulnerable: count may be zero
return sum / count;
}
// Vulnerable: Zero dimension in image processing
void vulnerable_resize_image(Image *img, int new_width, int new_height) {
// Vulnerable: dimensions may be zero
int x_ratio = img->width / new_width;
int y_ratio = img->height / new_height;
// ... resize logic
}
# Vulnerable: Zero divisor from user input
def vulnerable_calculate_rate(total, time_seconds):
# Vulnerable: time_seconds may be zero
return total / time_seconds
# Vulnerable: Zero length list
def vulnerable_average(values):
# Vulnerable: len(values) may be zero
return sum(values) / len(values)
// Vulnerable: Division without validation
public class VulnerableDivision {
public double vulnerablePercentage(int part, int total) {
// Vulnerable: total may be zero
return (part * 100.0) / total;
}
public int vulnerableItemsPerPage(int totalItems, int itemsPerPage) {
// Vulnerable: itemsPerPage may be zero
return totalItems / itemsPerPage;
}
}
Fixed Code
// Fixed: Check for zero divisor
int secure_calculate_average(int *values, int count) {
if (count <= 0) {
return 0; // Or return error code
}
int sum = 0;
for (int i = 0; i < count; i++) {
sum += values[i];
}
return sum / count;
}
// Fixed: Validate dimensions
int secure_resize_image(Image *img, int new_width, int new_height) {
if (new_width <= 0 || new_height <= 0) {
return -1; // Error: invalid dimensions
}
int x_ratio = img->width / new_width;
int y_ratio = img->height / new_height;
// ... resize logic
return 0;
}
# Fixed: Validate divisor
def secure_calculate_rate(total, time_seconds):
if time_seconds <= 0:
raise ValueError("Time must be positive")
return total / time_seconds
# Fixed: Check for empty list
def secure_average(values):
if not values:
raise ValueError("Cannot calculate average of empty list")
return sum(values) / len(values)
# Alternative: Return None for invalid input
def secure_average_optional(values):
if not values:
return None
return sum(values) / len(values)
// Fixed: Validate before division
public class SecureDivision {
public double securePercentage(int part, int total) {
if (total == 0) {
throw new IllegalArgumentException("Total cannot be zero");
}
return (part * 100.0) / total;
}
public int secureItemsPerPage(int totalItems, int itemsPerPage) {
if (itemsPerPage <= 0) {
throw new IllegalArgumentException("Items per page must be positive");
}
return totalItems / itemsPerPage;
}
// Alternative: Optional return type
public OptionalDouble safePercentage(int part, int total) {
if (total == 0) {
return OptionalDouble.empty();
}
return OptionalDouble.of((part * 100.0) / total);
}
}
CVE Examples
- CVE-2007-3268 — Invalid size value led to divide-by-zero.
- CVE-2007-2723 — Empty content triggered divide-by-zero.
- CVE-2007-2237 — Zero height value caused divide-by-zero.
References
- MITRE Corporation. "CWE-369: Divide By Zero." https://cwe.mitre.org/data/definitions/369.html