Skip to content

Commit f4e7bcf

Browse files
committed
Fix BigInt#testBit to throw ArithmeticException when n is negative.
The `_bigInteger` field, a mutable value initialized by `BigInt#bigInteger`, is accessed directly by `BigInt#testBit` when the input `n` is negative. As a result, `testBit` currently throws a `NullPointerException`. The expected behavior is for `testBit` to throw an `ArithmeticException` when `n` is negative, as specified by the underlying `java.math.BigInteger` API: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/BigInteger.html#testBit(int) This issue was not detected in `BigIntTest` because `_bigInteger` is initialized by earlier tests. Additionally, using `BigInt(42).testBit(-3)` for testing does not reproduce the issue, as `BigInt(42)` uses a cached instance where `_bigInteger` is already initialized. See: scala-native/scala-native#2897
1 parent dff2675 commit f4e7bcf

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

library/src/scala/math/BigInt.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ final class BigInt private (private var _bigInteger: BigInteger, private val _lo
379379
else if (_long < 0) BigInt(-1)
380380
else BigInt(0) // for _long >= 0
381381
} else BigInt(this.bigInteger.shiftRight(n))
382-
382+
383383
/** Bitwise and of BigInts
384384
*/
385385
def &(that: BigInt): BigInt =
@@ -501,7 +501,7 @@ final class BigInt private (private var _bigInteger: BigInteger, private val _lo
501501
(_long & (1L << n)) != 0
502502
else
503503
_long < 0 // give the sign bit
504-
} else _bigInteger.testBit(n)
504+
} else this.bigInteger.testBit(n)
505505

506506
/** Returns a BigInt whose value is equivalent to this BigInt with the designated bit set.
507507
*/

0 commit comments

Comments
 (0)