'Condition is always false' in 'BigInteger#multiply(BigInteger, boolean)'

Andrey Turbanov turbanoff at gmail.com
Sun Aug 15 09:12:09 UTC 2021


Hello.
I found suspicious condition in method
"java.math.BigInteger#multiply(java.math.BigInteger, boolean)"
It's detected by IntelliJ IDEA inspection 'Constant conditions & exceptions'

```
if (bitLength(mag, mag.length) +
    bitLength(val.mag, val.mag.length) >
    32L*MAX_MAG_LENGTH) {
    reportOverflow();
}
```

Method bitLength returns 'int', hence the left operand of '>'
comparison is 'int' too.
But the right operand of '>' comparison is 'long' with value ==
Integer.MAX_VALUE + 1.
It means this condition is always false.


Reproducer:
BigInteger a = new BigInteger(1073742825, ThreadLocalRandom.current());
BigInteger b = new BigInteger(1073742825, ThreadLocalRandom.current());
BigInteger c = a.multiply(b);




Andrey Turbanov


More information about the core-libs-dev mailing list