Sonar Violation: Security - Array is stored direct

2020-03-31 07:14发布

问题:

I did come across the following 2 posts about a similar issue Sonar Violation and Sonar Violation But, my problem is with byte[]

Following is the code snippet for which I get this warning in Sonar

public void setValue(byte[] value) {
    this.value = value;
}

I looked at the solutions and made the following changes

public void setValue(byte[] value) {
    if(value == null) {
        this.value = new byte[0];
    } else {
        this.value= Arrays.copyOf(value, value.length); 
    }
}

Even then, I get the same Security violation warning in Sonar. Am I getting this warning because it is byte[] and byte arrays need to be handled differently?

回答1:

You could use the following to resolve the issue with byte[]

value.clone()