Recent Posts
Archives

Posts Tagged ‘The user-supplied array … is stored directly’

PostHeaderIcon GWT / Sonar / Cannot invoke clone() on the array type… / The user-supplied array … is stored directly

I have to fix all critical “Violations” owing to Sonar.
Let’s consider a basic block:
[java] public void setSushis(float[] sushis) {
this.sushis = sushis;
}[/java]
This piece of code raises this error in Sonar:
[java] Security – Array is stored directly : The user-supplied array ‘sushis’ is stored directly.[/java]
In a naive step, I modified the code as this:
[java] public void setSushis(float[] sushis) {
this.sushis = sushis.clone();
}[/java]
This may have been OK… but, now, I get the following error within GWT compilation:
[java]Cannot invoke clone() on the array type float[][/java]

To fix the issue, I amended the code as follows:
[java] public void setSushis(float[] theSushis) {
this.sushis= new float[theSushis.length];
System.arraycopy(theSushis, 0, this.sushis, 0, theSushis.length);
}[/java]

Disclaimer: even though I followed Sonar’s advice and requirements, I am a little skeptic about this kind of “violation”. 😐