# VNX-783: Operator Precedence Logic Error

Unparenthesised expressions whose precedence differs from their evident intent.

| Field | Value |
|---|---|
| Rule ID | `VNX-783` |
| Severity | Low |
| SARIF level | `note` |
| Kind | `sast` |
| Languages | C, C++, Java, JavaScript / TypeScript, PHP, Python, C# |
| CWE | [CWE-783](https://cwe.mitre.org/data/definitions/783.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 | `operator-precedence` `cwe-783` |
| Source | [`internal/sast/rules/vnx-783.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-783.rego) |

## Overview

VNX-783 maps to [CWE-783: Operator Precedence Logic Error](https://cwe.mitre.org/data/definitions/783.html). It reports expressions that the language groups differently from what the code plainly intends, only in the languages where the grouping is actually wrong:

| Shape | Parsed as | Languages |
|---|---|---|
| `if (fd = open(p, O_RDONLY) < 0)` | `fd = (open(...) < 0)` | C, C++, JavaScript, PHP |
| `if n := len(d) > LIMIT:` | `n := (len(d) > LIMIT)` | Python |
| `flags & MASK == 0` | `flags & (MASK == 0)` | C, C++, JavaScript, PHP |
| `hi << 8 + lo` | `hi << (8 + lo)` | C, C++, Java, JavaScript, PHP, Python, C# |
| `!user.role === "admin"` | `(!user.role) === "admin"` | C, C++, JavaScript, PHP |
| `!err instanceof AppError` | `(!err) instanceof AppError` | JavaScript |
| `"Admin: " + isAdmin ? "yes" : "no"` | `("Admin: " + isAdmin) ? ...` | JavaScript, PHP (`.`) |
| `"Owner: " + v == null ? "none" : v` | `("Owner: " + v) == null ? ...` | Java, JavaScript, C# |
| `price + tax ?? 0` | `(price + tax) ?? 0` | JavaScript/TypeScript, C# |

Not reported: the parenthesised forms; Go bitwise and shift expressions, and Python `&` against `==`, where `&` and `<<` bind tighter than `==` and `+`; Java `&` on booleans; C++ stream insertion (`cout << 8 + n`); a negation compared with a boolean, or (in C and PHP) with 0 or null, where both groupings give the same answer; `$on = count($xs) > 0`, a deliberate flag; comments and strings.

## Why this matters

A precedence slip turns a security check into a constant: `if (!req.user.role === "admin")` is always false, so the guard never denies, and `fd = open(...) < 0` leaves `fd` holding 0 (stdin) or 1.

## What gets flagged

```c
if (st->st_mode & S_IWOTH != 0)        /* FLAGGED */
uint16_t v = p[0] << 8 + p[1];          /* FLAGGED */
```

```javascript
if (!req.user.role === "admin") {       // FLAGGED
```

Not flagged:

```c
if ((fd = open(path, O_RDONLY)) < 0)
if ((flags & O_NONBLOCK) == 0)
```

## Remediation

Parenthesise the intended grouping, or use the dedicated operator (`!==`, `!(x instanceof Y)`).

## References

- [CWE-783: Operator Precedence Logic Error](https://cwe.mitre.org/data/definitions/783.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-783
// nosec vnx-783
```

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