# VNX-704: File or content size narrowed from long with an unchecked cast

Reports Java casts to int, short, char or byte of a long file or content size (File.length(), Files.size(), FileChannel.size(), getContentLengthLong().

| Field | Value |
|---|---|
| Rule ID | `VNX-704` |
| Severity | Low |
| SARIF level | `note` |
| Kind | `sast` |
| Languages | Java |
| CWE | [CWE-704](https://cwe.mitre.org/data/definitions/704.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-type-conversion` `narrowing-cast` `cwe-704` |
| Source | [`internal/sast/rules/vnx-704.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-704.rego) |

## Overview

VNX-704 maps to [CWE-704: Incorrect Type Conversion or Cast](https://cwe.mitre.org/data/definitions/704.html). Java reports file, upload and response sizes as `long`. Casting one to `int` (or `short`, `char`, `byte`) silently wraps past the narrow type's range: a file over 2 GB allocates a negative or tiny buffer, which throws `NegativeArraySizeException` or reads a truncated file, and a size limit checked after the cast is passed by a value that exceeds it.

The rule reports, in `.java` files:

- `(int)`/`(short)`/`(char)`/`(byte)` applied directly to `File.length()` (a receiver declared `File`/`RandomAccessFile` in the file, a receiver named like `file`/`srcFile`, or `new File(...)`), `Files.size(...)`, `getFilePointer()`, `getContentLengthLong()`, `contentLength()`, `getSize()` and `getCompressedSize()`;
- such a cast of a variable declared `long` and assigned from one of those (or from a `.length()`/`.size()` call) in the 15 lines above.

Not reported: `Math.toIntExact(...)`, a variable compared with `Integer.MAX_VALUE` or any limit between the assignment and the cast, casts of parenthesised arithmetic, casts of int-valued calls (`String.length()`, stream `count()`, `Math.round`), comments and string literals. Narrowing of `Integer.parseInt`/`Long.parseLong` results is VNX-192.

## What gets flagged

```java
byte[] buf = new byte[(int) file.length()];             // FLAGGED
byte[] out = new byte[(int) Files.size(p)];             // FLAGGED
return new byte[(int) upload.getSize()];                // FLAGGED

long size = source.length();
return new byte[(int) size];                            // FLAGGED
```

## What is not flagged

```java
return new byte[Math.toIntExact(file.length())];

long size = Files.size(p);
if (size > Integer.MAX_VALUE) {
    throw new IllegalStateException("too large");
}
return new byte[(int) size];
```

## Remediation

1. Use `Math.toIntExact(...)`, which throws instead of wrapping.
2. Reject sizes above an explicit limit before converting, or stream the content instead of allocating one buffer for it.

## References

- [CWE-704: Incorrect Type Conversion or Cast](https://cwe.mitre.org/data/definitions/704.html)
- [CWE-197: Numeric Truncation Error](https://cwe.mitre.org/data/definitions/197.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-704
// nosec vnx-704
```

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