github / github/codeql

False negative: DoubleCheckedLocking.ql cannot detect scenarios with ternary expressions.

Aperta
#21,395 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
question
Lingua principale
CodeQL
Stelle
10.1k
Fork
2.1k
Merge medio
2g 15h
PR unite (30g)
141

Descrizione

**Version**
codeql 2.23.9
**Description of the issue**
When I detect the code like this using java/Likely Bugs/Concurrency/DoubleCheckedLocking.ql, the problem is reported normally:
```java

public class PosCase1 {
private Object instance; // Non-volatile field
public Object getInstance() {
if (instance == null) { // First null check
synchronized (this) { // [REPORTED LINE]
if (instance == null) { // Second null check inside synchronized block
instance = new Object(); // Initialization
}
}
}
return instance; // Return after double-checked locking
}
}
```

However, when ternary expressions are introduced into the code, DoubleCheckedLocking.ql fails to detect the problem:
```java
public class PosCase1_Var1 {
private Object instance; // Non-volatile field
public Object getInstance() {
// Use ternary for outer check, but preserve unsafe pattern
return (instance != null) ? instance : createInstance();
}
private Object createInstance() {
synchronized (this) {
if (instance == null) {
instance = new Object();
}
return instance;
}
}
}
```
These two code snippets are semantically identical, only using a ternary expression with some transformations, which is why DoubleCheckedLocking.ql cannot detect the problem.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start with java/Likely Bugs/Concurrency/DoubleCheckedLocking.ql and reproduce the two Java snippets from the issue to compare query results. Done means the query reports the unsafe ternary-based double-checked locking scenario while continuing to report the existing direct pattern.

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

Valutazione

Stack tecnologico
java
Ambito
devtools, security
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.