From c8e6a38693d73e06e59c69fbd40c3924836a10e9 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 17 Nov 2013 09:13:45 +1100 Subject: [PATCH] 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. --- src/libextra/num/bigint.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/libextra/num/bigint.rs b/src/libextra/num/bigint.rs index cd5ccc14cafb1..b79c2bd5cb565 100644 --- a/src/libextra/num/bigint.rs +++ b/src/libextra/num/bigint.rs @@ -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(÷r); 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]