Skip to content

Commit fecda58

Browse files
committed
java: understand more initializers
Whne a fiels is assigned a safe type in a constructor, that field is not exposed.
1 parent f5e6ca0 commit fecda58

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

java/ql/lib/semmle/code/java/ConflictingAccess.qll

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,23 @@ class ExposedField extends Field {
6363
not this.getType() instanceof LockType and
6464
// field is not thread-safe
6565
not isThreadSafeType(this.getType()) and
66-
not isThreadSafeType(this.getInitializer().getType()) and
66+
not isThreadSafeType(initialValue(this).getType()) and
6767
// the initializer guarantees thread safety
68-
not isThreadSafeInitializer(this.getInitializer())
68+
not isThreadSafeInitializer(initialValue(this))
6969
}
7070
}
7171

72+
/**
73+
* Gets the initial value for the field `f`.
74+
* This is either a static initializer or an assignment in a constructor.
75+
*/
76+
Expr initialValue(Field f) {
77+
result = f.getInitializer()
78+
or
79+
result = f.getAnAssignedValue() and
80+
result.getEnclosingCallable() = f.getDeclaringType().getAConstructor()
81+
}
82+
7283
/**
7384
* A field access that is exposed to potential data races.
7485
* We require the field to be in a class that is annotated as `@ThreadSafe`.

java/ql/test/query-tests/ThreadSafe/ThreadSafe.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,4 @@
4343
| examples/Test.java:60:5:60:10 | this.y | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/Test.java:60:5:60:10 | this.y | this expression |
4444
| examples/Test.java:74:5:74:10 | this.y | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/Test.java:74:5:74:10 | this.y | this expression |
4545
| examples/Test.java:74:14:74:14 | y | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/Test.java:74:14:74:14 | y | this expression |
46-
| examples/ThreadSafeInitializers.java:29:9:29:16 | sync_map | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/ThreadSafeInitializers.java:29:9:29:16 | sync_map | this expression |
47-
| examples/ThreadSafeInitializers.java:37:9:37:12 | cmap | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/ThreadSafeInitializers.java:37:9:37:12 | cmap | this expression |
4846
| examples/ThreadSafeInitializers.java:45:9:45:14 | this.y | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/ThreadSafeInitializers.java:45:9:45:14 | this.y | this expression |
49-
| examples/ThreadSafeInitializers.java:49:9:49:11 | set | This field access (publicly accessible via $@) is not protected by any monitor, but the class is annotated as @ThreadSafe. | examples/ThreadSafeInitializers.java:49:9:49:11 | set | this expression |

java/ql/test/query-tests/ThreadSafe/examples/ThreadSafeInitializers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public ThreadSafeInitializers() {
2626
}
2727

2828
public void sync_map_put(Integer i, Integer v) {
29-
sync_map.put(i,v); // $ SPURIOUS: Alert
29+
sync_map.put(i,v);
3030
}
3131

3232
public void sync_map_initialised_put(Integer i, Integer v) {
3333
sync_map_initialised.put(i,v);
3434
}
3535

3636
public void cmap_put(String s1, String s2) {
37-
cmap.put(s1, s2); // $ SPURIOUS: Alert
37+
cmap.put(s1, s2);
3838
}
3939

4040
public void cmap_initialised_put(String s1, String s2) {
@@ -46,7 +46,7 @@ public void setY(int y) {
4646
}
4747

4848
public void set_add(Integer i) {
49-
set.add(i); // $ SPURIOUS: Alert
49+
set.add(i);
5050
}
5151

5252
public void set_initialised_add(Integer i) {

0 commit comments

Comments
 (0)