Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion java/ql/src/Likely Bugs/Concurrency/StartInConstructor.ql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

import java

private predicate hasASubclass(RefType t) {
exists(RefType sub | sub != t | sub.getAnAncestor() = t)
}

/**
* Holds if this type is either `final` or
* `private` and without subtypes.
Expand All @@ -24,7 +28,11 @@ private predicate cannotBeExtended(RefType t) {
or
// If the class is private, all possible subclasses are known.
t.isPrivate() and
not exists(RefType sub | sub != t | sub.getAnAncestor() = t)
not hasASubclass(t)
or
// If the class only has private constructors, all possible subclasses are known.
forex(Constructor c | c.getDeclaringType() = t | c.isPrivate()) and
not hasASubclass(t)
}

from MethodCall m, Constructor c, Class clazz
Expand Down
16 changes: 15 additions & 1 deletion java/ql/test/query-tests/StartInConstructor/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ public Private() {
}

}
}

public static class AllPrivateConstructors {
Thread myThread;

private AllPrivateConstructors() {
myThread = new Thread("myThread");
// OK - class cannot be extended outside this file, and is not in fact extended
myThread.start();
}

public static AllPrivateConstructors create() {
return new AllPrivateConstructors();
}
}
}
Loading