-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Java/more thread safe initialisers #20910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR enhances Java thread safety analysis to recognize when fields are initialized with thread-safe types inside constructors, not just in field initializer expressions. This reduces false positives when fields are assigned thread-safe collections (like ConcurrentHashMap or synchronized maps) in constructors.
Key Changes:
- Added
initialValuepredicate to check both field initializers and constructor assignments - Modified
ExposedFieldclass to use the new predicate for more comprehensive thread-safety checking - Added comprehensive test coverage for the new functionality
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
java/ql/lib/semmle/code/java/ConflictingAccess.qll |
Implements the core logic by adding initialValue predicate and updating ExposedField to check constructor assignments |
java/ql/test/query-tests/ThreadSafe/examples/ThreadSafeInitializers.java |
Adds test cases demonstrating thread-safe initialization patterns in constructors |
java/ql/test/query-tests/ThreadSafe/ThreadSafe.expected |
Updates expected test results to include the new test case alert |
java/ql/src/change-notes/2025-11-25-thread-safe-initializers.md |
Documents the enhancement in the release notes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| /** | ||
| * Gets the initial value for the field `f`. | ||
| * This is either a static initializer or an assignment in a constructor. |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation says "This is either a static initializer or an assignment in a constructor" but should say "field initializer" instead of "static initializer". A static initializer is a method (<clinit>), whereas this predicate returns the field's initializer expression.
| * This is either a static initializer or an assignment in a constructor. | |
| * This is either a field initializer or an assignment in a constructor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree here. Let's fix the qldoc.
52296d4 to
efb6a79
Compare
|
The performance run shows something on |
|
dca looks fairly uneventful to me. |
Co-authored-by: Raúl Pardo <raul.pardo@protonmail.com>
Whne a fiels is assigned a safe type in a constructor, that field is not exposed.
a73233c to
cbc0100
Compare
Cool, then I feel ready to merge. I have fixed the QLDoc and rebased (to make CI happy). |
When a field is assigned a safe type in a constructor, that field is not exposed.