简体   繁体   English

为什么要返回this.variable不是一个漏洞?

[英]Why is returning this.variable not a vulnerability?

Sonar rule squid:S2384 raises an issue on this code: 声纳规则squid:S2384引发了这个代码的问题:

public Date getCreatedOn() {
    return createdOn;
}

following the rule Mutable members should not be stored or returned directly 遵循规则不应存储或直接返回Mutable成员

I understand that we should not return the original, instead we should return a copy of the object. 我明白我们不应该返回原文,而是应该返回该对象的副本。

On the other hand, Sonar does not raise an issue on this code: 另一方面,Sonar没有就此代码提出问题:

public Date getCreatedOn() {
    return this.createdOn;
}

What makes this code different? 是什么让这个代码不同?

Are we not returning the original copy in the 2nd case? 我们是不是在第二种情况下退回原件?

This is a deficiency in a way how SonarJava (Java static code analyzer used in SonarQube platform) detects this issue. 这是SonarJava(SonarQube平台中使用的Java静态代码分析器)检测到此问题的方式的缺陷。 I created following ticket to improve the implementation to cover cases when field is returned using this . 我创建了以下故障单以改进实现,以涵盖使用this字段返回字段时的情况。

https://jira.sonarsource.com/browse/SONARJAVA-2424 https://jira.sonarsource.com/browse/SONARJAVA-2424

the code you have shared is same. 您共享的代码是相同的。 but the link you have shared is different. 但是你分享的链接是不同的。

if a class have data-member of reference type and it is mutable and when we are returning directly we are referring the same object, so it can be modified. 如果一个类具有引用类型的数据成员并且它是可变的,并且当我们直接返回时,我们引用相同的对象,因此可以修改它。 It is better to return value of an object by another object. 最好用另一个对象返回一个对象的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我收到错误 this.variable is null in java class - Why I am getting error this.variable is null in java class 在我的Servlet中使用this.variable访问全局变量的目的是什么? - What is the purpose of accessing a global variable with this.variable across my servlet? SonarQube 漏洞:显式声明变量的可见性 - SonarQube vulnerability: Explicitly declare the visibility for variable Google为什么说我的代码仍然存在意图漏洞? - Why does Google say my code still has an Intent Vulnerability? 为什么此恶意代码漏洞 (Spotbugs) 的修复有效: - Why does this fix of a malicious code vulnerability (Spotbugs) work: 为什么在返回之前将 volatile 分配给局部变量 - Why would you assign volatile to local variable before returning 当 uri 变量不是 null 时,为什么 HttpUriRequest.getURI() 返回 null? - Why is HttpUriRequest.getURI() returning null when the uri variable is not null? 恶意代码漏洞-可能通过返回对可变对象的引用来公开内部表示形式-使用哪些对象? - Malicious code vulnerability - May expose internal representation by returning reference to mutable object - With what objects? 恶意代码漏洞 - 可能通过返回对可变对象的引用来暴露内部表示 - Malicious code vulnerability - May expose internal representation by returning reference to mutable object 恶意代码漏洞 - 可能通过返回对 int[] 的可变 object 的引用来暴露内部表示 - Malicious code vulnerability - May expose internal representation by returning reference to mutable object for int[]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM