Menu

#1324 MethodReturnsInternalArray false positive with clone()

PMD-5.3.0
closed
None
PMD
3-Major
Bug
5.2.3
2015-04-01
2015-03-24
Rei Angelus
No

Almost the same as issue 1299, but with an array clone instead of an array copy.

:::java
public Hit[] getHits() {
return hits == null ? null : hits.clone();
}

Rule:MethodReturnsInternalArray Priority:3 Returning 'getHits' may expose an internal array

This solution don't return error:
:::java
public Hit[] getHits() {
final Hit[] ret = hits == null ? null : Arrays.copyOf(hits, hits.length);
return ret;
}

Discussion

  • Rei Angelus

    Rei Angelus - 2015-03-24

    I will be more precise. You can test it and fail it with :

    :::xml
    <test-code>
    <description>#1324 MethodReturnsInternalArray false positive with clone() </description>
    <expected-problems>0</expected-problems>
    <![CDATA
    public class Test {
    private Hit[
    hits;

    public Hit[] getHits() {
        return hits == null ? null : hits.clone();
    }
    

    }
    ]]>

    </test-code>

    Beware, if you remove the line "private Hit[] hits;", the test will succeed.

     

    Last edit: Rei Angelus 2015-03-24
  • Andreas Dangel

    Andreas Dangel - 2015-03-25

    It's very similar to [#962] - the only difference is "this" in the return statement/ternary operator.

     

    Related

    Issues: #962

  • Andreas Dangel

    Andreas Dangel - 2015-03-25

    This will be fixed with the next version.

     
  • Andreas Dangel

    Andreas Dangel - 2015-03-25
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-Next
     

Log in to post a comment.