java/unsafe-double-checked-locking: missed when the field is re-read through a call that returns it
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
The reporter suspects getAFieldRead in the DoubleCheckedLocking.qll library, where a local only counts as a field read when its assignment source is a field access or another such local. Start by reading that predicate and the query java/unsafe-double-checked-locking that uses it, then run the query on the two samples seed/DclSeed.java and variant/DclVariant.java. Done means DclVariant.m() is reported on its synchronized block, as DclSeed.m() already is, without changing the result for the seed case.
由索引模型根據 Issue 內容生成。
描述
Description of the issue
The query reports double-checked locking on the non-volatile field x in DclSeed.m(): the field is read into a local z, checked against null outside and again inside the synchronized block, and initialised there. DclVariant.m() is the same except the read inside the synchronized block is java.util.Collections.singletonList(x).get(0), which just returns the field's value. Nothing is reported for DclVariant.
Probably getAFieldRead in DoubleCheckedLocking.qll: a local counts as a field read when its assignment source is a field access or another such local, so a value coming back through a call isn't followed, and the inner z == null is no longer a null check of x. SpotBugs' DC_DOUBLECHECK missed the same pair (spotbugs/spotbugs#4212).
Query: java/unsafe-double-checked-locking (codeql/java-queries 1.1.10)
CodeQL CLI: 2.27.2
Code samples
seed/DclSeed.java:
class DclSeed {
Object x;
void m() {
Object z = x;
if (z == null) {
synchronized (this) { // query fires here
z = x;
if (z == null) {
x = z = new Object();
}
}
}
}
}
variant/DclVariant.java:
class DclVariant {
Object x;
void m() {
Object z = x;
if (z == null) {
synchronized (this) { // query silent
z = java.util.Collections.singletonList(x).get(0);
if (z == null) {
x = z = new Object();
}
}
}
}
}
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 x is not thread-safe" on the synchronized block of DclVariant.m(), as on DclSeed.m().
See also https://github.com/github/codeql/issues/22967.
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
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
github/codeql 的其他 Issue
-
JavaScript externs model StorageEvent() with no type argument可能已有人在做 @asgerf 於 13 天前認領。 未關閉false-positive javascript
難度 2/5 1-3 小時 新手友好度 84/100
github/codeql#22632 · 3 則留言 · 1 個 reaction · 已指派 2 人 ·
維護者通常 1 天內回覆
-
Add AlertSuppression.ql for Rust (inline // codeql[...] suppression)可能已有人在做 @cnuss 於 214 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
false-positive
難度 2/5 1-3 小時 新手友好度 70/100
github/codeql#21076 · 3 則留言 · 3 個 reaction ·
維護者通常 1 天內回覆
-
java/non-final-call-in-constructor: a call made inside a lambda in the constructor is not reported未關閉
難度 4/5 3-5 天 新手友好度 45/100
維護者通常 1 天內回覆
-
java/local-shadows-field: a local declared inside a lambda or anonymous class body is not reported未關閉
難度 3/5 半天 新手友好度 55/100
維護者通常 1 天內回覆
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
feature
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
needs-triage
難度 2/5 1-3 小時 新手友好度 65/100
manaflow-ai/cmux#32617 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
ossf/wg-best-practices-os-developers#1193 ·
維護者通常 1 天內回覆
-
enhancement good first issue
難度 2/5 1-3 小時 新手友好度 78/100
laaielichter/Gulliver#22 ·
維護者通常 1 天內回覆