# VNX-369: Divide By Zero

A division or modulo whose divisor is a variable assigned from outside the program (a parsed number, a request value.

| Field | Value |
|---|---|
| Rule ID | `VNX-369` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | C, C++, Go, Java |
| CWE | [CWE-369](https://cwe.mitre.org/data/definitions/369.html) |
| CAPEC | [CAPEC-66](https://capec.mitre.org/data/definitions/66.html) |
| ATT&CK | [T1190](https://attack.mitre.org/techniques/T1190/) |
| CWSS vector | `CWSS:1.0/TI:M/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:M/DI:H/EX:M/EC:N/P:C` |
| Tags | `divide-by-zero` `cwe-369` |
| Source | [`internal/sast/rules/vnx-369.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-369.rego) |

## Overview

VNX-369 maps to [CWE-369: Divide By Zero](https://cwe.mitre.org/data/definitions/369.html). Integer division by zero panics in Go, throws in Java and is undefined behaviour in C, so a divisor an attacker controls is a denial-of-service primitive.

The rule finds the division operator in code only. A URL, a path, a purl, a string literal or a comment that contains a slash is not a division, and a divisor that is a constant or that the program computed itself is not reported.

## What gets flagged

```go
// FLAGGED: n comes from the request and is divided by with no zero check
n, _ := strconv.Atoi(r.FormValue("n"))
return total / n
```

```c
// FLAGGED: d comes from argv
int d = atoi(argv[1]);
return 100 / d;
```

```go
// FLAGGED: compound division
count, _ := strconv.Atoi(r.URL.Query().Get("count"))
sum /= count
```

## What is not flagged

```go
// guarded: a comparison with zero in the lines above
if n == 0 {
    return 0
}
return total / n

// not a division: the slash is inside a string or a comment
u := "https://example.com/api/request"
```

```java
// bounded below by one, or clamped on assignment
if (size < 1) throw new IllegalArgumentException("size");
int secs = Math.max(1, Integer.parseInt(req.getParameter("secs")));
```

Test files are skipped.

## Remediation

1. Reject or default a zero divisor before dividing, and return an error to the caller.
2. Bound the value at the point you parse it (`n < 1` is an error).

## References

- [CWE-369: Divide By Zero](https://cwe.mitre.org/data/definitions/369.html)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-369
// nosec vnx-369
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-369 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
