VNX-697: Incorrect comparison: compareTo() against 1/-1, equals(null), or equality with NaN
Reports Java comparisons that cannot mean what they say: compareTo()/compare() tested with == or != against 1 or -1, x.equals(null), and == or != against Double.NaN or Float.NaN.
| Field | Value |
|---|---|
| Rule ID | VNX-697 |
| Severity | Low |
| SARIF level | note |
| Kind | sast |
| Languages | Java |
| CWE | CWE-697 |
| CAPEC | CAPEC-66 |
| ATT&CK | 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-comparison cwe-697 |
| Source | internal/sast/rules/vnx-697.rego |
Overview
VNX-697 maps to CWE-697: Incorrect Comparison. A comparison that compiles but does not test what the author meant makes a limit, ordering or validity check take the wrong branch silently. The rule reports three Java shapes that are wrong by the language’s own contracts:
- compareTo()/compare() against 1 or -1.
compareTo,compareToIgnoreCaseandcompare(Comparator,Integer.compare,Long.compare…) promise only the sign of their result.String.compareToreturns the character difference and many comparators return other magnitudes, soamount.compareTo(limit) == 1is false for values that are over the limit. Both operand orders are reported. - x.equals(null). A correct
equalsreturns false for null, and whenxis null the call throws, so the expression never detects a null. - Equality with NaN.
r == Double.NaNis always false and!=always true.
Not reported: sign tests (> 0, < 0, == 0, >= 1), comparisons with other constants, Double.isNaN, x == null, Objects.equals(x, null), test files (where equals(null) checks the equals contract), comments and string literals. Timing-unsafe secret comparison is VNX-385 and VNX-CRYPTO-008; String == is VNX-597; boxed == is VNX-595.
What Gets Flagged
return amount.compareTo(limit) == 1; // FLAGGED
return -1 == byPrice.compare(a, b); // FLAGGED
if (c.getRegion().equals(null)) { ... } // FLAGGED
if (r == Double.NaN) { ... } // FLAGGED
What Is Not Flagged
return amount.compareTo(limit) > 0;
if (Double.isNaN(r) || tag == null) { ... }
Remediation
- Test the sign of
compareTo/compare:> 0,< 0,== 0. - Use
x == null(orObjects.isNull(x)) to detect null. - Use
Double.isNaN(x)/Float.isNaN(x).
References
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 ;):
# nosec vnx-697
// nosec vnx-697
Or record an ignore rule: vulnetix ignore add --rule vnx-697 --reason "...". See Suppressing findings.