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

Brian Burkhalter brian.burkhalter at oracle.com
Mon Aug 16 18:13:01 UTC 2021


Hello Andrey,

Indeed that appears to be incorrect. I filed [1] to track it.

Thanks,

Brian

[1] https://bugs.openjdk.java.net/browse/JDK-8272541

On Aug 15, 2021, at 2:12 AM, Andrey Turbanov <turbanoff at gmail.com<mailto:turbanoff at gmail.com>> wrote:

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);



More information about the core-libs-dev mailing list