Repository navigation
[java] Fix #6747: NonThreadSafeSingleton flags ternary lazy init - #6938
Merged
UncleOwen merged 4 commits intoAug 10, 2026
Merged
Conversation
What:
NonThreadSafeSingletonRule now also detects the check-then-act lazy init
race when it is written as a ternary assignment instead of an if statement:
field = field == null ? new T() : field;
Why:
The rule previously only matched the `if (field == null) field = new T();`
shape, because visit(ASTMethodDeclaration) scanned descendants of
ASTIfStatement. A ternary assignment is an ASTAssignmentExpression whose
right operand is an ASTConditionalExpression, so it lives outside any
ASTIfStatement and the race went unreported (false negative). The two
forms have identical concurrency semantics.
How:
After the existing if-statement scan, iterate the method's assignment
expressions and flag those whose left-hand side is a tracked field and
whose right-hand side is a conditional expression that null-checks the
same field. Synchronized blocks are skipped, mirroring the existing
if-statement handling. Adds two XML test cases (positive: 2 violations
on the ternary form; negative: 0 violations when the ternary guard has
no null check).
Signed-off-by: 付典 <[email protected]>
Contributor
|
Compared to main: (comment created at 2026-08-10 13:15:26+00:00 for dd43ee8) |
UncleOwen
requested changes
Aug 9, 2026
…#6747) Address review feedback on pmd#6938: - Extract a private fieldWriteName(ASTAssignmentExpression) helper that encapsulates the "non-synchronized write to a tracked singleton field" check. The if-statement and ternary check-then-act detection now share this helper, so both cases apply identical field-write criteria and cannot drift apart. The if case still accepts any tracked field in the condition (its existing conservative behavior is left untouched); the ternary case keeps requiring the condition field to equal the assignment target, since that is what makes `f = f == null ? ... : f` a check-then-act race. - Move the pmd#6747 release notes entry from java-errorprone to a new java-multithreading category (NonThreadSafeSingleton is a multithreading rule). Signed-off-by: 付典 <[email protected]>
…md#6747) Further to extracting fieldWriteName, also extract isNullCheckOnField for the condition check. The if-statement and ternary cases had two near-identical condition-check blocks that differed only in fields.contains(...) (if case, any tracked field) vs fieldName.equals(...) (ternary case, same field read and written) -- exactly the kind of subtle difference that is hard to spot. Both now call isNullCheckOnField(condition, targetField), where targetField is null for the if case and the assignment target for the ternary case. Behavior is unchanged; the if case still accepts any tracked field in the condition (its long-standing conservative behavior), the ternary case still requires the condition field to equal the assignment target. Signed-off-by: 付典 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
NonThreadSafeSingletonnow also flags the ternary form of the unsafe lazy-init race:which is semantically identical to the already-detected
if (field == null) field = new T();.Closes #6747.
Why
The rewrite exposes the exact same check-then-act data race under concurrent access, yet produced 0 findings where the
ifform produced 2. The rule only walkedASTIfStatement, so the ternary assignment (anASTAssignmentExpressionwhose right-hand side is anASTConditionalExpression) was invisible to it.How
In
visit(ASTMethodDeclaration), after the existingASTIfStatementloop, add a second pass overASTAssignmentExpressionnodes that reports a violation when:synchronizedblock), andThis mirrors the checks already applied to the
ifform. Theif-form path is unchanged: a plainfield = new T();assignment has a non-conditional right-hand side, so the two passes don't double-report.Testing
NonThreadSafeSingleton.xml:field = ready ? new T() : field;) → 0 findings.expected: <2> but was: <0>) and passes after (13/13)../mvnw -pl pmd-java checkstyle:check→ 0 violations.Added a
release_notes.mdentry under the java notes.