Skip to content

[java] Fix #6747: NonThreadSafeSingleton flags ternary lazy init - #6938

Merged
UncleOwen merged 4 commits into
pmd:mainfrom
fudianchn:fix/nonthreadsafesingleton-ternary-6747
Aug 10, 2026
Merged

UncleOwen merged 4 commits into
pmd:mainfrom
fudianchn:fix/nonthreadsafesingleton-ternary-6747

Conversation

@fudianchn

Copy link
Copy Markdown
Contributor

What

NonThreadSafeSingleton now also flags the ternary form of the unsafe lazy-init race:

field = field == null ? new T() : field;

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 if form produced 2. The rule only walked ASTIfStatement, so the ternary assignment (an ASTAssignmentExpression whose right-hand side is an ASTConditionalExpression) was invisible to it.

How

In visit(ASTMethodDeclaration), after the existing ASTIfStatement loop, add a second pass over ASTAssignmentExpression nodes that reports a violation when:

  • the assignment target is a tracked field (not inside a synchronized block), and
  • the right-hand side is a ternary whose condition null-checks the same field.

This mirrors the checks already applied to the if form. The if-form path is unchanged: a plain field = new T(); assignment has a non-conditional right-hand side, so the two passes don't double-report.

Testing

  • Added two XML cases to NonThreadSafeSingleton.xml:
    • the ternary form from the issue (2 expected findings, lines 6 and 11), and
    • a ternary without a null check (field = ready ? new T() : field;) → 0 findings.
  • Verified the test fails before the fix (expected: <2> but was: <0>) and passes after (13/13).
  • ./mvnw -pl pmd-java checkstyle:check → 0 violations.

Added a release_notes.md entry under the java notes.

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]>
@pmd-actions-helper

pmd-actions-helper Bot commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-08-10 13:15:26+00:00 for dd43ee8)

Comment thread docs/pages/release_notes.md Resolved
fudianchn and others added 3 commits August 9, 2026 23:51
…#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]>
@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 10, 2026

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@UncleOwen
UncleOwen merged commit ff15eb5 into pmd:main Aug 10, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] NonThreadSafeSingleton: False negative with ternary conditional operator

2 participants