# VNX-733: Compiler Optimization Removal or Modification of Security-critical Code

Reports C and C++ code the optimiser may delete: a memset/bzero of a secret buffer that nothing reads before it is freed or goes out of scope.

| Field | Value |
|---|---|
| Rule ID | `VNX-733` |
| Severity | Low |
| SARIF level | `note` |
| Kind | `sast` |
| Languages | C, C++ |
| CWE | [CWE-733](https://cwe.mitre.org/data/definitions/733.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 | `compiler-removal` `dead-store` `undefined-behaviour` `cwe-733` |
| Source | [`internal/sast/rules/vnx-733.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-733.rego) |

## Overview

VNX-733 maps to [CWE-733](https://cwe.mitre.org/data/definitions/733.html). It reports two shapes:

- **Dead-store wipe.** `memset`/`bzero`/`ZeroMemory`/`RtlZeroMemory` with a zero fill, of a buffer whose name marks it as a secret: password, passphrase, secret, key, seed, pin, token, credential or plaintext, with prefixes such as `session_key` and camelCase forms such as `masterKey`. The next statement frees the buffer, or, for a local array, returns or closes the scope. Nothing reads the cleared memory afterwards, so the compiler may remove the clear.
- **Pointer-wraparound check.** `if (p + n < p)` or `p + n <= p` on a name declared as a pointer. Pointer overflow is undefined behaviour, so the compiler may fold the test to false.

## What gets flagged

```c
memset(password, 0, 128);     // FLAGGED: dead store
free(password);

if (buf + off < buf)          // FLAGGED: UB, may be removed
    return -1;
```

Not reported:

- wipes the compiler must keep: `explicit_bzero`, `memset_s`, `SecureZeroMemory`, `OPENSSL_cleanse` and `sodium_memzero`
- a clear followed by a use of the buffer
- clears of struct members or pointer parameters before a return
- non-zero fills
- integer overflow checks such as `a + b < a` on `size_t`
- test code and comments

VNX-CWE1037-AUTO reports the dead-store wipe as well.

## Remediation

1. Wipe secrets with `explicit_bzero`, `memset_s`, `SecureZeroMemory` or `OPENSSL_cleanse`.
2. Write bounds checks on lengths, for example `n > (size_t)(end - p)`, and do not test pointer arithmetic for overflow.

## References

- [CWE-733](https://cwe.mitre.org/data/definitions/733.html)
- [CWE-14: Compiler Removal of Code to Clear Buffers](https://cwe.mitre.org/data/definitions/14.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-733
// nosec vnx-733
```

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