加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 推论ArrayIsSredredDirectly直接规则的PMD

发布时间:2020-12-14 17:48:43 所属栏目:Java 来源:网络整理
导读:PMD在Sun Security规则集中有一个名为ArrayIsStoredDirectly的规则: Constructors and methods receiving arrays should clone objects and store the copy. This prevents that future changes from the user affect the internal functionality. 这是他们
PMD在Sun Security规则集中有一个名为ArrayIsStoredDirectly的规则:

Constructors and methods receiving arrays should clone objects and store the copy. This prevents that future changes from the user affect the internal functionality.

这是他们的例子:

public class Foo {
 private String [] x;
  public void foo (String [] param) {
      // Don't do this,make a copy of the array at least
      this.x=param;
  }
}

我不认为我完全理解这个规则背后的原因.是因为传递的数组中的值可以在其他地方更改吗?传递Collection与传递数组有什么区别?

解决方法

问题是调用者可以保留它所传递的数组参数的副本,然后可以更改其内容.如果对象是安全关键的,并且通过不受信任的代码进行调用,则会有一个安全漏洞.

在这种情况下,传递集合并将其保存而不复制它也将是潜在的安全风险. (我不知道是否有PMD规则告诉你这个.)

在这两种情况下,解决风险的方法(如果是真实的)是将属性设置为参数数组或集合的副本.另一方面,如果您知道呼叫方永远是受信任的代码,那么这个副本是浪费时间的,更好的解决方案是告诉PMD对这种特定方法的安静.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读