Fix multiplication logic in decimal parsing functions.

Signed-off-by: Jean Pierre Dudey <jeandudey@hotmail.com>
This commit is contained in:
Jean Pierre Dudey 2018-08-22 07:22:47 -04:00
parent e48e559740
commit a915bc194d
1 changed files with 16 additions and 6 deletions

View File

@ -143,9 +143,14 @@ impl Decimal {
match *b {
b'-' => { negative = true; }
b'0'...b'9' => {
match 10i64.overflowing_mul(mantissa + (b - b'0') as i64) {
(_, true) => return Err(ParseDecimalError::TooBig),
(n, false) => mantissa = n,
match 10i64.checked_mul(mantissa) {
None => return Err(ParseDecimalError::TooBig),
Some(n) => {
match n.checked_add((b - b'0') as i64) {
None => return Err(ParseDecimalError::TooBig),
Some(n) => mantissa = n,
}
}
}
if past_dec { exponent += 1; }
}
@ -296,9 +301,14 @@ impl UDecimal {
for b in s.as_bytes() {
match *b {
b'0'...b'9' => {
match 10u64.overflowing_mul(mantissa + (b - b'0') as u64) {
(_, true) => return Err(ParseDecimalError::TooBig),
(n, false) => mantissa = n,
match 10u64.checked_mul(mantissa) {
None => return Err(ParseDecimalError::TooBig),
Some(n) => {
match n.checked_add((b - b'0') as u64) {
None => return Err(ParseDecimalError::TooBig),
Some(n) => mantissa = n,
}
}
}
if past_dec { exponent += 1; }
}