Repository navigation
[java] Fix #6901: MethodReturnsInternalArray: Track internal array escapes through expressions - #6940
Conversation
|
Compared to main: (comment created at 2026-09-24 08:26:08+00:00 for 13db35e) |
UncleOwen
left a comment
There was a problem hiding this comment.
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.
e39d4e1 to
df9342a
Compare
|
Implemented in Local values use Both review examples are regression tests:
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 Local |
68276c8 to
b701ef0
Compare
Signed-off-by: 付典 <[email protected]>
b701ef0 to
174cd15
Compare
|
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
left a comment
There was a problem hiding this comment.
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).
Signed-off-by: 付典 <[email protected]>
|
@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. |
this.data = Collections.singletonList(a).get(0)
#7341
AI disclosure: this change was prepared with AI coding agents, reviewed and revised line by line by me.
Describe the PR
MethodReturnsInternalArraynow 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:byte[] result = data; return result;return flag ? data : new byte[0];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
DataflowPassto 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 originalCollections.singletonList(data).get(0)reproducer is an additional case, alongsideList.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, andArrays.copyOfRangeremain 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
68276c8b95has 30 new violations versusmain, 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?
./mvnw clean verifypasses (checked automatically by github actions)