Malicious code vulnerability - May expose internal representation by returning reference to mutable object(恶意代码漏洞 - 可能通过返回对可变对象的引用来暴露内部表示)
问题描述
您好,我的违规行为如下:
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.
这篇关于恶意代码漏洞 - 可能通过返回对可变对象的引用来暴露内部表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:恶意代码漏洞 - 可能通过返回对可变对象的引用来暴露内部表示


基础教程推荐
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01