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

オープン
#22,967 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
java
領域
devtools, security

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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)
主要言語
CodeQL
スター
10.3k
フォーク
2.1k
平均マージ
2日 4時間
マージ済み PR(30日)
118

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

github/codeql のほかの issue

github/codeql の issue をすべて見る

似ている issue

DevTools の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。