diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-11-17 09:13:45 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-11-17 16:05:01 +1100 |
| commit | c8e6a38693d73e06e59c69fbd40c3924836a10e9 (patch) | |
| tree | 3234ec2e814f410492bbb87fd54f8a467097b2b0 /src | |
| parent | 90754ae9c95c18841c0200d77da917af5ecde5ee (diff) | |
| download | rust-c8e6a38693d73e06e59c69fbd40c3924836a10e9.tar.gz rust-c8e6a38693d73e06e59c69fbd40c3924836a10e9.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libextra/num/bigint.rs | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/libextra/num/bigint.rs b/src/libextra/num/bigint.rs index cd5ccc14caf..b79c2bd5cb5 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] |
