# VNX-609: Double-Checked Locking

Reports Java double-checked lazy initialisation (check, synchronized, same check) on a field of the same file that is not declared volatile.

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

## Overview

VNX-609 maps to [CWE-609: Double-Checked Locking](https://cwe.mitre.org/data/definitions/609.html). It reports a `synchronized (...)` block immediately preceded by `if (x == null)`, `if (null == x)` or `if (!flag)` and whose first statement repeats the same check, where `x` is a field declared in the same file without `volatile`.

Not reported: a `volatile` field, the local-variable idiom (`Helper h = helper; if (h == null) ...`) over a volatile field, a single check inside a synchronized method or block, `AtomicReference` checks, the holder-class idiom, and fields declared in another file.

## Why this matters

Without `volatile`, the Java memory model lets another thread see the reference assigned before the constructor's writes are visible, so it uses a half-built object; with a boolean flag it can see the flag set before the data the flag guards.

## What gets flagged

```java
private static Registry instance;

if (instance == null) {
    synchronized (Registry.class) {      // flagged
        if (instance == null) {
            instance = new Registry();
```

## Remediation

1. Declare the field `volatile`.
2. Or use the initialisation-on-demand holder class, an `enum` singleton, or eager initialisation.
3. For a confirmed false positive, add a `// vulnetix-ignore: VNX-609` comment on the line.

## References

- [CWE-609: Double-Checked Locking](https://cwe.mitre.org/data/definitions/609.html)
- [The "Double-Checked Locking is Broken" Declaration](https://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.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-609
// nosec vnx-609
```

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