GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Fix #6747: NonThreadSafeSingleton flags ternary lazy init by fudianchn · Pull Request #6938 · pmd/pmd

Conversation

@fudianchn

@fudianchn fudianchn commented Aug 5, 2026

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@fudianchn
[java] Fix pmd#6747: NonThreadSafeSingleton flags ternary lazy init
b57bb66
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 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

UncleOwen
UncleOwen requested changes Aug 9, 2026
View reviewed changes
Comment thread docs/pages/release_notes.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread .../main/java/net/sourceforge/pmd/lang/java/rule/multithreading/NonThreadSafeSingletonRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

fudianchn and others added 3 commits August 9, 2026 23:51
@fudianchn
[java] NonThreadSafeSingleton: extract shared field-write helper (pmd…
240ceb0
…#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]>
@fudianchn
[java] NonThreadSafeSingleton: also extract shared null-check helper (p…
d27b27d
…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
Merge branch 'main' into fork/fudianchn/fix/nonthreadsafesingleton-te…
dd43ee8
…rnary-6747
@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 10, 2026
UncleOwen
UncleOwen approved these changes Aug 10, 2026
View reviewed changes

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

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.

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@UncleOwen
UncleOwen merged commit ff15eb5 into pmd:main Aug 10, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@UncleOwen UncleOwen UncleOwen approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

7.27.0

Development

Successfully merging this pull request may close these issues.

[java] NonThreadSafeSingleton: False negative with ternary conditional operator

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@fudianchn @UncleOwen
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.