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

未關閉
#22,967 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
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 小時
30 天內合併 PR
118

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

github/codeql 的其他 Issue

查看 github/codeql 的全部 Issue

相似的 Issue

更多 DevTools Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。