| name | cwe-190-integer-overflow | |||||
|---|---|---|---|---|---|---|
| description | Use this skill when you need to remediate CWE-190 (Integer Overflow) vulnerabilities in Java code. Triggers on SAST findings, security reviews, or when fixing integer overflow issues. | |||||
| version | 1.0.0 | |||||
| license | MIT | |||||
| tags |
|
Integer Overflow
Reference: https://cwe.mitre.org/data/definitions/190.html
OWASP Category: A03:2021 – Injection
// VULNERABLE: Standard arithmetic can overflow silently
int userQuantity = Integer.parseInt(request.getParameter("quantity"));
int unitPrice = 100;
int total = userQuantity * unitPrice; // Overflow wraps to negative!
// Array allocation with overflow
int size = width * height; // Can overflow
byte[] buffer = new byte[size];Why it's vulnerable: This pattern is vulnerable to Integer Overflow
// SECURE: Use Math.xxxExact() methods that throw on overflow
int userQuantity = Integer.parseInt(request.getParameter("quantity"));
int unitPrice = 100;
try {
int total = Math.multiplyExact(userQuantity, unitPrice);
int withTax = Math.addExact(total, Math.multiplyExact(total, taxRate) / 100);
} catch (ArithmeticException e) {
throw new IllegalArgumentException("Quantity too large", e);
}
// For array allocations
try {
int size = Math.multiplyExact(width, height);
if (size > MAX_ALLOWED_SIZE) {
throw new IllegalArgumentException("Size exceeds limit");
}
byte[] buffer = new byte[size];
} catch (ArithmeticException e) {
throw new IllegalArgumentException("Dimensions cause overflow", e);
}Why it's secure: Implements proper protection against Integer Overflow
Look for these patterns in your codebase:
# Find arithmetic with parsed input
grep -rn "parseInt\|parseLong" --include="*.java" -A5 | grep -E "\*|\+"-
Use Math.addExact(), Math.subtractExact(), Math.multiplyExact()
-
Catch ArithmeticException and handle gracefully
-
Validate input ranges before arithmetic operations
-
Use BigInteger for arbitrary precision when needed
import java.lang.Math;
import java.math.BigInteger;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
Fix CWE-190 vulnerability
Resolve Integer Overflow issue
Secure this Java code against integer overflow
SAST reports CWE-190
| Layer | Files | Patterns |
|---|
| Controller | *Controller.java | User input handling |
| Service | *Service.java | Business logic |
| Repository | *Repository.java | Data access |
Source: Generated by Java CWE Security Skills Generator Last Updated: 2026-03-07