[corelib] Properly check radix in BigInt.toRadixString.

Change-Id: Ieb1b541db3c35984f4a75b618f9df1c82f4d37da
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/221201
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Commit-Queue: Stephen Adams <sra@google.com>
This commit is contained in:
Stephen Adams
2021-11-25 22:53:53 +00:00
committed by commit-bot@chromium.org
parent a98368c250
commit 12dae45e5a
5 changed files with 39 additions and 27 deletions
@@ -2905,7 +2905,7 @@ class _BigIntImpl implements BigInt {
* The [radix] argument must be an integer in the range 2 to 36.
*/
String toRadixString(int radix) {
if (radix > 36) throw RangeError.range(radix, 2, 36);
if (radix < 2 || radix > 36) throw RangeError.range(radix, 2, 36);
if (_used == 0) return "0";
@@ -2816,7 +2816,7 @@ class _BigIntImpl implements BigInt {
///
/// The [radix] argument must be an integer in the range 2 to 36.
String toRadixString(int radix) {
if (radix > 36) throw new RangeError.range(radix, 2, 36);
if (radix < 2 || radix > 36) throw RangeError.range(radix, 2, 36);
if (_used == 0) return "0";
+1 -1
View File
@@ -2493,7 +2493,7 @@ class _BigIntImpl implements BigInt {
* The [radix] argument must be an integer in the range 2 to 36.
*/
String toRadixString(int radix) {
if (radix > 36) throw new RangeError.range(radix, 2, 36);
if (radix < 2 || radix > 36) throw new RangeError.range(radix, 2, 36);
if (_used == 0) return "0";
+18 -12
View File
@@ -228,8 +228,10 @@ testModInverse() {
var m = BigInt.parse(
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
radix: 16);
var r = BigInt.parse("95929095851002583825372225918533539673793386278"
"360575987103577151530201707061", radix: 10);
var r = BigInt.parse(
"95929095851002583825372225918533539673793386278"
"360575987103577151530201707061",
radix: 10);
test(x, m, r);
}
@@ -830,18 +832,10 @@ void testToRadixString() {
}
}
var illegalRadices = [-1, 0, 1, 37];
for (var radix in illegalRadices) {
try {
new BigInt.from(42).toRadixString(radix);
Expect.fail("Exception expected");
} on ArgumentError catch (e) {
// Nothing to do.
}
}
// Try large numbers (regression test for issue 15316).
var bignums = [
BigInt.parse("0x8"),
BigInt.parse("0x80"),
BigInt.parse("0x80000000"),
BigInt.parse("0x100000000"),
BigInt.parse("0x10000000000000"),
@@ -871,6 +865,18 @@ void testToRadixString() {
bignum, result, "${bignum.toRadixString(16)} -> $digits/$radix");
}
}
const illegalRadices = [-2, -1, 0, 1, 37];
for (final bignum in bignums) {
for (final radix in illegalRadices) {
try {
bignum.toRadixString(radix);
Expect.fail("Exception expected for .toRadixString($radix)");
} on ArgumentError catch (e) {
// Nothing to do.
}
}
}
}
testToString() {
+18 -12
View File
@@ -230,8 +230,10 @@ testModInverse() {
var m = BigInt.parse(
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
radix: 16);
var r = BigInt.parse("95929095851002583825372225918533539673793386278"
"360575987103577151530201707061", radix: 10);
var r = BigInt.parse(
"95929095851002583825372225918533539673793386278"
"360575987103577151530201707061",
radix: 10);
test(x, m, r);
}
@@ -832,18 +834,10 @@ void testToRadixString() {
}
}
var illegalRadices = [-1, 0, 1, 37];
for (var radix in illegalRadices) {
try {
new BigInt.from(42).toRadixString(radix);
Expect.fail("Exception expected");
} on ArgumentError catch (e) {
// Nothing to do.
}
}
// Try large numbers (regression test for issue 15316).
var bignums = [
BigInt.parse("0x8"),
BigInt.parse("0x80"),
BigInt.parse("0x80000000"),
BigInt.parse("0x100000000"),
BigInt.parse("0x10000000000000"),
@@ -873,6 +867,18 @@ void testToRadixString() {
bignum, result, "${bignum.toRadixString(16)} -> $digits/$radix");
}
}
const illegalRadices = [-2, -1, 0, 1, 37];
for (final bignum in bignums) {
for (final radix in illegalRadices) {
try {
bignum.toRadixString(radix);
Expect.fail("Exception expected for .toRadixString($radix)");
} on ArgumentError catch (e) {
// Nothing to do.
}
}
}
}
testToString() {