MethodReturnsInternalArray false positive with clone()
A source code analyzer
Brought to you by:
adangel,
juansotuyo
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;
}
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;
}
]]>
</test-code>
Beware, if you remove the line "private Hit[] hits;", the test will succeed.
Last edit: Rei Angelus 2015-03-24
It's very similar to [#962] - the only difference is "this" in the return statement/ternary operator.
Related
Issues:
#962This will be fixed with the next version.