问题描述
您好,我的违规行为如下:
Hi I'm getting the violation as below:
恶意代码漏洞 - 可能会通过以下方式暴露内部表示返回对可变对象的引用
Malicious code vulnerability - May expose internal representation by returning reference to mutable object
在我的代码中我是这样写的
in my code i wrote like this
public String[] chkBox() {
return chkBox;
}
我们如何解决它.
推荐答案
正如错误消息所述,您正在返回内部状态(chkBox 很可能是对象内部状态的一部分,即使您不是显示它的定义)
As the error message states, you're returning internal state (chkBox is - most likely - part of the internal state of an object even though you're not showing its definition)
这可能会导致问题 - 例如 - 这样做
This can cause problems if you - for example - do
String[] box = obj.chkBox();
box[0] = null;
由于数组对象和所有 Java 对象一样,都是通过引用传递的,这也会改变存储在对象中的原始数组.
Since an array object, as all Java objects, is passed by reference, this will change the original array stored inside your object as well.
您最可能想要解决此问题的方法很简单
What you most likely want to do to fix this is a simple
return (String[])chkBox.clone();
返回数组的副本而不是实际的数组.
which returns a copy of the array instead of the actual array.
这篇关于恶意代码漏洞 - 可能通过返回对可变对象的引用来暴露内部表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!