Skip to content

Commit

Permalink
Fixes silent overflow when parsing Decimal::MAX with a fractional com…
Browse files Browse the repository at this point in the history
…ponent (#530)
  • Loading branch information
paupino committed Jul 18, 2022
1 parent aff1b9f commit afd41f7
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ fn handle_full_128<const POINT: bool, const NEG: bool, const ROUND: bool>(
if ROUND {
if digit >= 5 {
data += 1;

// Make sure that the mantissa isn't now overflowing
if overflow_128(data) {
return tail_error("Invalid decimal: overflow from mantissa after rounding");
}
}
} else {
return Err(Error::Underflow);
Expand Down Expand Up @@ -891,4 +896,13 @@ mod test {
Err(Error::from("Invalid decimal: no digits found"))
);
}

#[test]
fn from_str_edge_cases_6() {
// Decimal::MAX + 0.99999
assert_eq!(
parse_str_radix_10("79_228_162_514_264_337_593_543_950_335.99999"),
Err(Error::from("Invalid decimal: overflow from mantissa after rounding"))
);
}
}

0 comments on commit afd41f7

Please sign in to comment.