java/unsafe-double-checked-locking: not reported when the outer guard is written as !(!(x == null))

Aperta
#22,967 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java
Ambito
devtools, security

Direzione di ricerca

Start from the java/unsafe-double-checked-locking query in the java-queries pack and the guard logic it uses to recognise the outer helper == null check. Reproduce with the two sample files, Dcl.java and DclV.java, in a test directory for that query. The gap is that !(!(helper == null)) is not treated as equivalent to helper == null. Done means DclV is reported like Dcl, with a test case added for the double-negated form.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Description of the issue

The query reports double-checked locking on a non-volatile field in Dcl.getHelper(). In DclV.getHelper() the outer guard is !(!(helper == null)) instead of helper == null, with the inner check and the field unchanged. Nothing is reported for DclV, though it's the same test.

Query: java/unsafe-double-checked-locking (codeql/java-queries 1.1.10)

CodeQL CLI: 2.27.2

Code samples

seed/Dcl.java:

class Dcl {
    private Helper helper;
    Helper getHelper() {
        if (helper == null) {                 // query fires here
            synchronized (this) {
                if (helper == null) {
                    helper = new Helper();
                }
            }
        }
        return helper;
    }
    static class Helper {
        int x;
    }
}

variant/DclV.java:

class DclV {
    private Helper helper;
    Helper getHelper() {
        if (!(!(helper == null))) {           // equivalent outer guard; query silent
            synchronized (this) {
                if (helper == null) {
                    helper = new Helper();
                }
            }
        }
        return helper;
    }
    static class Helper {
        int x;
    }
}

How to reproduce

codeql database create db --language=java --build-mode=none \
    --source-root=<one directory> --overwrite
codeql database analyze db \
    codeql/java-queries:codeql-suites/java-security-and-quality.qls \
    --format=csv --output=out.csv
# one database per directory

Expected result

"Double-checked locking on the non-volatile field helper is not thread-safe" on DclV, as on Dcl.

Output of the commands above:

### CodeQL 2.27.2  query: java/unsafe-double-checked-locking
### suite: java-security-and-quality.qls, --build-mode=none, per-file isolated databases
### extraction JDK: openjdk version "21.0.11" 2026-04-21 LTS

--- SEED -- expected: query fires (seed/)
"Double-checked locking is not thread-safe","A repeated check on a non-volatile field is not thread-safe, and

--- VARIANT -- expected: query fires, actual: not reported (the bug) (variant/)
    (query reported nothing)
Lingua principale
CodeQL
Stelle
10.3k
Fork
2.1k
Merge medio
2g 4h
PR unite (30g)
118

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di github/codeql

Tutte le issue di github/codeql

Issue simili

Altre issue su DevTools

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.