# VNX-481: Assigning instead of Comparing

Detects an if or while condition that is a bare assignment, if (x = y), in C, C++ and Java.

| Field | Value |
|---|---|
| Rule ID | `VNX-481` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | C, C++, Java |
| CWE | [CWE-481](https://cwe.mitre.org/data/definitions/481.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 | `assignment-condition` `cwe-481` |
| Source | [`internal/sast/rules/vnx-481.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-481.rego) |

## Overview

VNX-481 maps to [CWE-481: Assigning instead of Comparing](https://cwe.mitre.org/data/definitions/481.html). `if (uid = 0)` overwrites `uid` and tests the assigned value, so the branch is never taken and the variable now holds the wrong value; `if (isAdmin = true)` makes every user an admin. The rule reports an `if` or `while` whose condition starts with `lvalue = expression` (including `*p = ...`, `s->field = ...`, `a[i] = ...`).

Not reported: the conventional deliberate form with an extra pair of parentheses (`if ((p = next()))`, `while ((c = getc(f)) != EOF)`), comparisons (`==`, `<=`, `>=`, `!=`), the comma operator (`while (p = q, q = next(q), p)`), the C++17 init-statement (`if (err = f(); err != 0)`), assignments in `for` headers and ordinary statements, and conditions in strings and comments.

## What gets flagged

```c
if (uid = 0) {               /* FLAGGED */
while (n->state = 1) {       /* FLAGGED */
```

```java
if (isAdmin = true) {        // FLAGGED
```

## What is not flagged

```c
while ((c = getc(in)) != EOF)
if ((p = malloc(16)))
if (a == b)
```

## Remediation

1. Use `==` for comparison.
2. When the assignment is intended, move it to its own statement or wrap it in a second pair of parentheses and compare explicitly: `if ((p = next()) != NULL)`.
3. Compile C/C++ with `-Wparentheses` (part of `-Wall`).

## References

- [CWE-481: Assigning instead of Comparing](https://cwe.mitre.org/data/definitions/481.html)
- [SEI CERT EXP45-C: Do not perform assignments in selection statements](https://wiki.sei.cmu.edu/confluence/display/c/EXP45-C.+Do+not+perform+assignments+in+selection+statements)

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

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