Menu

#1104 "Null passed for nonnull parameter" false positive

2.0.3
closed-fixed
5
2014-08-24
2012-08-06
Lukas Eder
No

Consider this piece of Java code:

--------------------------------------------------------
import java.math.BigInteger;

public class Test {
void x() {
BigInteger i = BigInteger.ZERO;

if (i == null) {
i = BigInteger.ZERO;
} else {
i = BigInteger.ZERO;
}

System.out.println(new BigInteger("1").add(i));
^^^^ FindVugs detects a bug here
}
}
--------------------------------------------------------

The detected bug reads

> Bug: Null passed for nonnull parameter of java.math.BigInteger.add(BigInteger) in Test.x()

The actual problem, however, is the useless null check, as BigInteger.ZERO can never be null.

Discussion

  • William Pugh

    William Pugh - 2012-08-10
    • status: open --> open-accepted
     
  • William Pugh

    William Pugh - 2012-09-27

    OK, to fix this I had to tell findBugs that BigDecimal.ZERO is always nonnull.

    Without knowing that, FindBugs was actually being fairly clever to report the warning correctly.

     
  • William Pugh

    William Pugh - 2012-09-27
    • status: open-accepted --> closed-fixed
     
  • Lukas Eder

    Lukas Eder - 2012-09-29

    Thanks for fixing this. Was it only related to BigInteger.ZERO? Because I feel that possibly, all static final values could be affected...

     
  • Lukas Eder

    Lukas Eder - 2012-09-29
    • status: closed-fixed --> open-fixed
     
  • William Pugh

    William Pugh - 2013-10-31
    • status: open-fixed --> closed-fixed
    • Group: --> 2.0.3
     
  • William Pugh

    William Pugh - 2013-10-31

    If the code had used field Foo.BAR, then you would want a bug reported here. It is only a false positive because BigInteger.ZERO is known to be non null.

     

Log in to post a comment.