mirror of
https://github.com/github/codeql.git
synced 2026-04-29 10:45:15 +02:00
Java: Add 2 double-checked-locking queries.
This commit is contained in:
75
java/ql/test/query-tests/DoubleCheckedLocking/A.java
Normal file
75
java/ql/test/query-tests/DoubleCheckedLocking/A.java
Normal file
@@ -0,0 +1,75 @@
|
||||
public class A {
|
||||
public class B {
|
||||
public int x = 2;
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
}
|
||||
|
||||
private String s;
|
||||
public String getString() {
|
||||
if (s == null) {
|
||||
synchronized(this) {
|
||||
if (s == null) {
|
||||
s = "string"; // OK, immutable
|
||||
}
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
private B b1;
|
||||
public B getter1() {
|
||||
B x = b1;
|
||||
if (x == null) {
|
||||
synchronized(this) {
|
||||
if ((x = b1) == null) {
|
||||
b1 = new B(); // BAD, not volatile
|
||||
x = b1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
private volatile B b2;
|
||||
public B getter2() {
|
||||
B x = b2;
|
||||
if (x == null) {
|
||||
synchronized(this) {
|
||||
if ((x = b2) == null) {
|
||||
b2 = new B(); // OK
|
||||
x = b2;
|
||||
System.out.println("OK");
|
||||
}
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
private volatile B b3;
|
||||
public B getter3() {
|
||||
if (b3 == null) {
|
||||
synchronized(this) {
|
||||
if (b3 == null) {
|
||||
b3 = new B();
|
||||
b3.x = 7; // BAD, post update init
|
||||
}
|
||||
}
|
||||
}
|
||||
return b3;
|
||||
}
|
||||
|
||||
private volatile B b4;
|
||||
public B getter4() {
|
||||
if (b4 == null) {
|
||||
synchronized(this) {
|
||||
if (b4 == null) {
|
||||
b4 = new B();
|
||||
b4.setX(7); // BAD, post update init
|
||||
}
|
||||
}
|
||||
}
|
||||
return b4;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
| A.java:25:7:30:7 | stmt | Double-checked locking on the non-volatile field $@ is not thread-safe. | A.java:21:13:21:14 | b1 | b1 |
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Concurrency/DoubleCheckedLocking.ql
|
||||
@@ -0,0 +1,2 @@
|
||||
| A.java:55:11:55:22 | ...=... | Potential race condition. This assignment to $@ is visible to other threads before the subsequent statements are executed. | A.java:50:22:50:23 | b3 | b3 |
|
||||
| A.java:68:11:68:22 | ...=... | Potential race condition. This assignment to $@ is visible to other threads before the subsequent statements are executed. | A.java:63:22:63:23 | b4 | b4 |
|
||||
@@ -0,0 +1 @@
|
||||
Likely Bugs/Concurrency/DoubleCheckedLockingWithInitRace.ql
|
||||
Reference in New Issue
Block a user