Skip to content

Commit

Permalink
Auto merge of #188 - koverstreet:master, r=cuviper
Browse files Browse the repository at this point in the history
bigint: Fix calculation of size for multiply temporaries

When x.len() and y.len() are both equal and odd, we have x1.len() + y1.len() + 1
(the required size to multiply x1 and y1) > y.len() + 1

This fixes #187
  • Loading branch information
homu committed Apr 20, 2016
2 parents 39c1de8 + 1e65e9d commit ffaee11
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions bigint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,9 +991,11 @@ fn mac3(acc: &mut [BigDigit], b: &[BigDigit], c: &[BigDigit]) {
let (x0, x1) = x.split_at(b);
let (y0, y1) = y.split_at(b);

/* We reuse the same BigUint for all the intermediate multiplies: */

let len = y.len() + 1;
/*
* We reuse the same BigUint for all the intermediate multiplies and have to size p
* appropriately here: x1.len() >= x0.len and y1.len() >= y0.len():
*/
let len = x1.len() + y1.len() + 1;
let mut p = BigUint { data: vec![0; len] };

// p2 = x1 * y1
Expand Down Expand Up @@ -3827,6 +3829,15 @@ mod biguint_tests {
}
}

#[test]
fn test_mul_overflow() {
/* Test for issue #187 - overflow due to mac3 incorrectly sizing temporary */
let s = "531137992816767098689588206552468627329593117727031923199444138200403559860852242739162502232636710047537552105951370000796528760829212940754539968588340162273730474622005920097370111";
let a: BigUint = s.parse().unwrap();
let b = a.clone();
let _ = a.checked_mul(&b);
}

#[test]
fn test_checked_div() {
for elm in MUL_TRIPLES.iter() {
Expand Down

0 comments on commit ffaee11

Please sign in to comment.