[
M C RCN] Nullcheck of value previously dereferenced
[RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE]
A value is checked here to see whether it is null, but this value can't be
null because it was previously dereferenced and if it were null a null pointer
exception would have occurred at the earlier dereference. Essentially, this code
and the previous dereference disagree as to whether this value is allowed to be
null. Either the check is redundant or the previous dereference is
erroneous.
先看一段代码:
public
class
MyTest {
private
String str
=
"123"
;
public
void
setStr(String str){
this
.str
=
str;
}
public
String getStr(){
return
this
.str;
}
public
void
test(){
String str2
=
"
123
"
;
synchronized
(str) {
if
(str
!=
null
) {
str2
=
"
123"
;
}
else
{
str2
=
"
456"
;
}
System.out.println(str2);
}
}
}
这个时候这段代码就会报Nullcheck of value previously dereferenced这个Bug,看Bug定位,发现问题出现在synchronized
(str) 这里,str没有检查是否为NULL?!OK,我现在改用
getStr()这个函数代替直接使用str,即:
synchronized
(
getStr()
),重新Find
Bug......居然没有发现错误-_-,但事实上getStr()并没有检查str是否为Null!!
现在我换另外一种写法,代码如下:
public
class
MyTest {
private
String str
=
"
123
"
;
public
void
setStr(String str){
this
.str
=
str;
}
public
String getStr(){
return
this
.str;
}
public
void
test(){
String str2
=
"
123
"
;
if
(str
!=
null
){ //tag2
synchronized
(str) {
if
(str
!=
null
) { //tag1
str2
=
"
123
"
;
}
else
{
str2
=
"
456
"
;
}
System.out.println(str2);
}
}
}
}
这次我在tag2处加了一行检查str是否为NULL的代码
,看FindBugs结果,出现了另外一个中等BUG:[M D RCN]Redundant nullcheck of value known to be non-null,跟踪发现是tag1处
的代码是多余的,因为tag2处已经检查了一遍,并且在
synchronized
(str)后,str被独占,它不可能被修改,也就是说
synchronized
(str)后,根本不需要检查str是否为空,tag1
处
的代码是多余的。如果把tag1处代码去掉,
[M D RCN]
警告就没有了。
不知道大家有没有发现,上面的代码还有个问题,看这段代码:
if
(str
!=
null
){
synchronized
(str) {
System.out.println(“
123
”);
}
}
如果是多线程运行时,你不能排除它会if(str != null)和synchronized(str)之间进行线程切换,然后把str至为null!所以上面这样写其实也是有可能出现问题的!只是FindBugs没有找出来。
我觉得最好的写法还是:
public
void
test()
throws
Exception{
try
{
String str2
=
"
123
"
;
synchronized
(
getStr()
) {
str2
=
"
456
"
;
System.out.println(str2);
}
}
catch
(Exception ex){
throw
ex;
}
//Do other things....
}
其实synchronized
(
getStr()
) 换成
synchronized
(
str
) 也是可以的,FindBugs不会再报Bug了。
总结一下,我觉
得FindBugs之所以会报[H C RCN] Nullcheck of value previously
dereferenced,是因为我没有检查str的值是否为Null,并且没有注意对可能出现的Exception的截获。而简单使用
getStr(),不检查str的值,不作异常捕获,也能躲过这个Bug,我觉得可能是FindBugs的一个BUG:<,因为估计它会认
为,getStr()这个函数里面会检查str的值......但这个仅仅是个人认为而已。
分享到:
相关推荐
computational undertakings of previously unprecedented scale and complexity. Entire fields and industries have sprung up as a result. This development is still ongoing, and it is creating new ...
Previously: 1. SSL connection support (finally!). Now you can enjoy Medis with Redis Labs & Azure. 2. Allow duplicating favorites. 3. Change the code font to "Consolas" to make the text clearer. 4. ...
The value for key CFBundleShortVersionString [1.2] in the Info.plist file must contain a higher version than that of the previously approved version [1.15] 明明1.2的版本号要大于1.15,怎么还会出现错误...
MESH2D was previously released under a GNU license. This has been removed to cater for the current restrictions of the MATLAB file exchange. I apologise to the many users who have been unable to ...
and event 2 corresponds to the reception of that message by a different computation, then we can always note that 1 ! 2, since a message cannot be received before it is sent. In this problem you will ...
Check out the boxed set that brings together Volumes 1 - 4A in one elegant case, and offers the purchaser a $50 discount off the price of buying the four volumes individually. The Art of Computer ...
PSU 10.2.0.5.12 includes all fixes previously included in PSU 10.2.0.5.11 and those listed in Section 7, "Bugs Fixed by This Patch". To install the PSU 10.2.0.5.12 patch, the Oracle home must have ...
cp beans apple dir2 把beans、apple文件复制到dir2目录下 cp -i beans apple 增加是否覆盖的提示 mv 移动或重命名文件/目录 用法 mv [-f] [-i] f1 f2 mv [-f] [-i] f1 ... fn d1 mv [-f] [-i] d1 d2 mv ...
We will do this by discussing loose types and their challenges, and show some examples of how these challenges were being tackled previously and why TypeScript is a better choice. Chapter 2, Getting...
B.2. Out of Bounds Tracking 61 OBT Programming 61 Information Category 61 Turning OBT On and Off 61 B.3. Function Keys 62 FKEYS Programming 62 Information Category 62 B.4. Tilt 62 TILT Programming 63 ...
rootroot@cm-System-Product-Name...Selecting previously unselected package libencode-locale-perl. (正在读取数据库 ... 系统当前共安装有 213805 个文件和目录。) Preparing to unpack .../libencode-locale-perl_...
Create and deliver high value reports to show the value of your System Center investment Gain access to real world solutions in the art and science of report planning and creation using System Center ...
Finally, put your skills to the test in a bigger challenge - designing and constructing a complex strategy game that will draw on and develop all your previously learned skills. What you will learn ...
2, the transmission ratio range of the expanded two-stage gear reducer ia=8 ~ 40, the transmission ratio range of v-belt transmission is ib=2~4, so the theoretical transmission ratio range is=16~160....
This book contains content previously published in Beginning JavaScript Charts. Create all kinds of charts using the latest technologies available on browsers Full of step-by-step examples, Create ...
previously set variable or directive (e.g. ${foo}) ; Expressions in the INI file are limited to bitwise operators and parentheses: ; | bitwise OR ; ^ bitwise XOR ; & bitwise AND ; ~ bitwise NOT ; ! ...
DOCUMENT, PREVIOUSLY PUBLISHED BY BELLCORE, MAY STILL REFLECT THE FORMER NAME AS IT WAS EMBEDDED IN THE DOCUMENTATION UNDER A PRIOR LICENSE FROM THE OWNERS OF THE BELL TRADEMARK, WHICH LICENSE HAS NOW...