Skip to content

Commit e094350

Browse files
committed
auto merge of #10523 : huonw/rust/10522, r=cmr
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.
2 parents 5208332 + c8e6a38 commit e094350

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/libextra/num/bigint.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ impl ToStrRadix for BigUint {
660660
let divider = FromPrimitive::from_uint(base).unwrap();
661661
let mut result = ~[];
662662
let mut m = n;
663-
while m > divider {
663+
while m >= divider {
664664
let (d, m0) = m.div_mod_floor(&divider);
665665
result.push(m0.to_uint().unwrap() as BigDigit);
666666
m = d;
@@ -2520,6 +2520,11 @@ mod bigint_tests {
25202520
check("-10", Some(-10));
25212521
check("Z", None);
25222522
check("_", None);
2523+
2524+
// issue 10522, this hit an edge case that caused it to
2525+
// attempt to allocate a vector of size (-1u) == huge.
2526+
let x: BigInt = from_str("1" + "0".repeat(36)).unwrap();
2527+
let _y = x.to_str();
25232528
}
25242529

25252530
#[test]

0 commit comments

Comments
 (0)