Java 数组Security

MethodReturnsInternalArray: Exposing internal arrays directly allows the user to modify some code that could be critical. It is safer to return a copy of the array.

翻译   方法返回内部数组:暴露内部数组直接允许用户修改的代码会是非常危险的,返回一个数组的copy是安全的做法

代码示例:

public class SecureSystem {

  UserData [] ud;

  public UserData [] getUserData() {

      // Don't return directly the internal array, return a copy

      return ud;

  }

}

应当使用

return Arrays.copyOf(ud, ud.length);



·  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.

翻译   数组被直接存储:构造器和方法接收数组应该clone对象并保存副本,这会阻止用户将来的改变影响内部的功能。

代码示例:

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;

  }

}

应当使用

    if (null != param) {
            this.x= Arrays.copyOf(param, param.length);
        } else {
            this.x= null;
        }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值