extra: handle an edge case in BigUint.to_str().

If any of the digits was one past the maximum (e.g. 10**9 for base 10),
then this wasn't detected correctly and so the length of the digit was
one more than expected, causing a very large allocation.

Fixes #10522.
Fixes #10288.
This commit is contained in:
Huon Wilson 2013-11-17 09:13:45 +11:00
parent 90754ae9c9
commit c8e6a38693

View File

@ -660,7 +660,7 @@ impl ToStrRadix for BigUint {
let divider = FromPrimitive::from_uint(base).unwrap();
let mut result = ~[];
let mut m = n;
while m > divider {
while m >= divider {
let (d, m0) = m.div_mod_floor(&divider);
result.push(m0.to_uint().unwrap() as BigDigit);
m = d;
@ -2520,6 +2520,11 @@ mod bigint_tests {
check("-10", Some(-10));
check("Z", None);
check("_", None);
// issue 10522, this hit an edge case that caused it to
// attempt to allocate a vector of size (-1u) == huge.
let x: BigInt = from_str("1" + "0".repeat(36)).unwrap();
let _y = x.to_str();
}
#[test]