GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |
GeezerVison is showing github.com — [java] Fix #6747: NonThreadSafeSingleton flags ternary lazy init by fudianchn · Pull Request #6938 · pmd/pmd Conversation
Contributor
What
field = field == null ? new T() : field; which is semantically identical to the already-detected Closes #6747. WhyThe rewrite exposes the exact same check-then-act data race under concurrent access, yet produced 0 findings where the HowIn
This mirrors the checks already applied to the Testing
Added a
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsWhat:
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]>
|
There was an error while loading. Please reload this page.
|
Compared to main: (comment created at 2026-08-10 13:15:26+00:00 for dd43ee8) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…#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]>
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
[java] NonThreadSafeSingleton: False negative with ternary conditional operator
There was an error while loading. Please reload this page.