Skip to content

[java] Fix #6901: MethodReturnsInternalArray: Track internal array escapes through expressions - #6940

Merged
adangel merged 3 commits into
pmd:mainfrom
fudianchn:fix/methodreturnsinternalarray-singletonlist-6901
Sep 24, 2026
Merged

adangel merged 3 commits into
pmd:mainfrom
fudianchn:fix/methodreturnsinternalarray-singletonlist-6901

Conversation

@fudianchn

@fudianchn fudianchn commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

AI disclosure: this change was prepared with AI coding agents, reviewed and revised line by line by me.

Describe the PR

MethodReturnsInternalArray now detects indirect returns of internal arrays through local aliases and conditional expressions, including local variables assigned in different branches. These common cases are the main improvements:

  • Local alias: byte[] result = data; return result;
  • Conditional return: return flag ? data : new byte[0];
  • Branch assignments: if (flag) { result = data; } else { result = new byte[0]; } return result;

Each can expose the internal array without copying it. Returning a defensive copy such as data.clone() remains unreported. These cases are covered by the rule's regression tests.

The rule traces array-valued return expressions backwards, using DataflowPass to resolve local aliases and merged control-flow definitions. For calls, provenance crosses a declaration only when the generic return type is connected to the receiver or a formal parameter through the same type variable. The original Collections.singletonList(data).get(0) reproducer is an additional case, alongside List.of(data, data).get(1) and generic tuple accessors, without an API-specific escape allowlist.

Calls to exact helpers in the current class are summarized from their return expressions with array arguments bound to formal parameters. A helper returning its argument or an internal field therefore propagates the source, while a helper returning a clone does not. Array clone(), Arrays.copyOf, and Arrays.copyOfRange remain freshness barriers.

The analysis is a may-analysis. It does not model heap mutation, dynamically dispatched helper bodies, or non-generic calls across class boundaries. This bounded propagation avoids treating every call argument as a possible return value.

The regression report for 68276c8b95 has 30 new violations versus main, down from 43 in the previous revision. All 30 were inspected: 26 are expected internal-array returns, and four are conservative results. Three involve zero-length static arrays whose declarations are unavailable in the current compilation unit, and one comes from a boolean-controlled helper where both return paths are summarized. No API-specific exceptions were added for these cases.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@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 30 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-09-24 08:26:08+00:00 for 13db35e)

@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.

The moment we merge this PR as it is, someone does List.of(data, data).get(1). Or org.apache.commons.lang3.Tuple.of(data, data).getLeft(). Again, there's a million-and-one ways to do this.

This needs some kind of data flow analysis to find if an internal array escapes.

@fudianchn
fudianchn marked this pull request as draft August 12, 2026 00:37
@fudianchn
fudianchn force-pushed the fix/methodreturnsinternalarray-singletonlist-6901 branch from e39d4e1 to df9342a Compare August 12, 2026 02:23
@fudianchn fudianchn changed the title [java] Fix #6901: MethodReturnsInternalArray detects singletonList(x).get(0) escape [java] Fix #6901: Track internal array escapes through expressions Aug 12, 2026
@fudianchn

fudianchn commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Implemented in 68276c8b95 with backward provenance analysis.

Local values use DataflowPass reaching definitions. Calls propagate provenance only where the generic declaration connects an input type variable to the return type, so this is based on the declaration contract rather than an API allowlist. Exact helpers in the current class are summarized from their return expressions with arguments bound to formal parameters. This distinguishes identity(data) from a helper that returns data.clone().

Both review examples are regression tests:

  • List.of(data, data).get(1)
  • Tuple.of(data, data).getLeft()

The final regression run reports 30 additions, down from 43. I inspected all 30: 26 are expected internal-array returns, while four are bounded conservative results from unavailable cross-compilation-unit declarations or a helper with two possible return paths. The previous 19 false positives caused by propagating every call argument are gone. The final report's violation details are identical to the inspected 4e7b2fb3 report.

Local ./mvnw clean verify passed across all 44 modules. GitHub compile, unit tests on Linux/macOS/Windows, verify, dogfood, documentation, spelling, and regressiontester are all green.

@fudianchn
fudianchn marked this pull request as ready for review August 12, 2026 06:24
@fudianchn
fudianchn marked this pull request as draft August 12, 2026 06:26
@fudianchn
fudianchn marked this pull request as ready for review August 12, 2026 07:12
@fudianchn
fudianchn marked this pull request as draft August 15, 2026 03:18
@fudianchn
fudianchn force-pushed the fix/methodreturnsinternalarray-singletonlist-6901 branch from 68276c8 to b701ef0 Compare August 15, 2026 03:18
@fudianchn
fudianchn marked this pull request as ready for review August 15, 2026 03:35
@fudianchn
fudianchn force-pushed the fix/methodreturnsinternalarray-singletonlist-6901 branch from b701ef0 to 174cd15 Compare August 15, 2026 14:01
@fudianchn

Copy link
Copy Markdown
Contributor Author

History note: the branch was squashed to a single commit (b701ef0) on Aug 15 and rebased onto main today as 174cd15 to resolve the release-notes conflict. The inspection comment above referencing 68276c8 / 4e7b2fb describes the same content that is now contained in that single commit. MethodReturnsInternalArrayTest: 59/59 pass on 174cd15.

@adangel adangel 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 for the PR and the rewrite to use data flow!

This actually fixes a couple of more false negatives (like local aliases, ternary conditions, ...)

I think, these fixes deserve a better representation in the release notes. All of them can be seen in the regression tester. The original issue #6901 doesn't appear once...

I'd suggest to recycle #6901 to rename this to sth. like "[java] MethodReturnsInternalArray: Various false negatives with local aliases, conditions, ...". These are the important fixes you did. And these should be described in #6901.

The singletonList example would then be just one marginal example, as IMHO it's a very bad code sample (why write return Collections.singletonList(data).get(0) instead of just return data? Maybe to avoid writing @SuppressWarnings?).

I'll try to do this on Thursday (unless someone beats me to it).

@adangel adangel added this to the 7.28.0 milestone Sep 21, 2026
@fudianchn
fudianchn marked this pull request as draft September 21, 2026 17:03
@fudianchn
fudianchn marked this pull request as ready for review September 21, 2026 17:04
@fudianchn

Copy link
Copy Markdown
Contributor Author

@adangel I gave this a try: I updated the PR description to focus on local aliases, conditional expressions, and branch assignments, kept the singletonList example as an additional case, and updated the release notes to match. The release-note change is in a separate commit so it is easier to review. I'm not sure whether this is what you had in mind. Does this capture the scope you wanted?

@adangel

adangel commented Sep 21, 2026

Copy link
Copy Markdown
Member

@adangel I gave this a try: I updated the PR description to focus on local aliases, conditional expressions, and branch assignments, kept the singletonList example as an additional case, and updated the release notes to match. The release-note change is in a separate commit so it is easier to review. I'm not sure whether this is what you had in mind. Does this capture the scope you wanted?

Thanks. Can you also update #6901 accordingly? It should have the title adjusted and the description should list examples of the false negatives that the are now found.
If you can't update #6901 directly, you can add a comment with your proposed description and one of us can copy it later to the description.

@adangel adangel 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!

@adangel adangel changed the title [java] Fix #6901: Track internal array escapes through expressions [java] Fix #6901: MethodReturnsInternalArray: Track internal array escapes through expressions Sep 24, 2026
@adangel
adangel merged commit 4b3af04 into pmd:main Sep 24, 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] MethodReturnsInternalArray: Various false negatives with local aliases and conditional expressions

3 participants