cwe-369-divide-by-zero

SKILL.md

CWE-369 Divide By Zero

Description

Divide By Zero

Reference: https://cwe.mitre.org/data/definitions/369.html

OWASP Category: A03:2021 – Injection


Vulnerable Pattern

❌ Example 1: Vulnerable Pattern

// VULNERABLE: No check for zero divisor
int result = total / count;

Why it's vulnerable: This pattern is vulnerable to Divide By Zero


Deterministic Fix

✅ Secure Implementation: Secure Implementation

// SECURE: Check divisor before division
if (count == 0) {
    throw new IllegalArgumentException("Division by zero");
    // Or return default value
    // return 0;
}
int result = total / count;

Why it's secure: Implements proper protection against Divide By Zero


Detection Pattern

Look for these patterns in your codebase:

# Find division operations
grep -rn "/[^/*]\\|% " --include="*.java" | grep -v "//"

Remediation Steps

  1. Always check divisor is not zero before division

  2. Handle zero case explicitly with default value or exception


Key Imports


Verification

After remediation:

  • Run SAST scanner to confirm vulnerability is resolved

  • Review all instances of the vulnerable pattern

  • Add unit tests that verify the secure implementation

  • Check for similar patterns in related code


Trigger Examples

Fix CWE-369 vulnerability
Resolve Divide By Zero issue
Secure this Java code against divide by zero
SAST reports CWE-369

Common Vulnerable Locations

Layer Files Patterns

| Controller | *Controller.java | User input handling |

| Service | *Service.java | Business logic |

| Repository | *Repository.java | Data access |


References


Source: Generated by Java CWE Security Skills Generator Last Updated: 2026-03-07

Weekly Installs
1
First Seen
11 days ago
Installed on
mcpjam1
claude-code1
replit1
junie1
windsurf1
zencoder1