# VNX-682: Incorrect Calculation

Detects calculations that are wrong for every input: integer division or remainder by a literal zero in C, C++ and Java.

| Field | Value |
|---|---|
| Rule ID | `VNX-682` |
| Severity | Low |
| SARIF level | `note` |
| Kind | `sast` |
| Languages | C, C++, Java |
| CWE | [CWE-682](https://cwe.mitre.org/data/definitions/682.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:L/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:L/DI:H/EX:L/EC:N/P:L` |
| Tags | `incorrect-calculation` `cwe-682` |
| Source | [`internal/sast/rules/vnx-682.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-682.rego) |

## Overview

VNX-682 maps to [CWE-682: Incorrect Calculation](https://cwe.mitre.org/data/definitions/682.html). Most incorrect calculations depend on intent, which a line-oriented rule cannot know. This rule reports the two shapes that are wrong whatever the input.

## What gets flagged

```c
return hits / 0;        // FLAGGED: undefined behaviour in C
int rem = hits % 0;     // FLAGGED
x /= 0;                 // FLAGGED
```

```java
long bucket = sum % 0L;                              // FLAGGED: ArithmeticException
return Math.abs(key.hashCode()) % shards;            // FLAGGED: negative for Integer.MIN_VALUE
int idx = Math.abs(random.nextInt()) % items.length; // FLAGGED
```

## What is not flagged

- Floating-point division by `0.0`, hexadecimal or non-zero divisors, and division by a variable (VNX-369 reports unchecked external divisors).
- `Math.floorMod(h, n)`, a masked hash (`h & 0x7fffffff`) and `Math.abs(r.nextInt(n))`.
- Test code, where dividing by zero is how an exception is provoked.
- Comments and string literals.

## Remediation

Fix the divisor, and compute a bucket with `Math.floorMod(key.hashCode(), n)` or `(key.hashCode() & 0x7fffffff) % n`.

## References

- [CWE-682: Incorrect Calculation](https://cwe.mitre.org/data/definitions/682.html)
- [SEI CERT INT33-C: Ensure that division and remainder operations do not result in divide-by-zero errors](https://wiki.sei.cmu.edu/confluence/display/c/INT33-C.+Ensure+that+division+and+remainder+operations+do+not+result+in+divide-by-zero+errors)

## 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-682
// nosec vnx-682
```

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