Skip to content

Commit

Permalink
Fix panic when parsing improper scientific notation. (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx authored Feb 21, 2025
1 parent 46fb4c3 commit 722af9f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ impl Decimal {

if let Some(stripped) = exp.strip_prefix('-') {
let exp: u32 = stripped.parse().map_err(|_| Error::from(ERROR_MESSAGE))?;
if exp > Self::MAX_SCALE {
return Err(Error::ScaleExceedsMaximumPrecision(exp));
}
ret.set_scale(current_scale + exp)?;
} else {
let exp: u32 = exp.parse().map_err(|_| Error::from(ERROR_MESSAGE))?;
Expand Down
10 changes: 9 additions & 1 deletion tests/decimal_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3373,10 +3373,18 @@ fn it_can_parse_scientific_notation() {
("12345E-28", "0.0000000000000000000000012345"),
("1.2345E0", "1.2345"),
("1E28", "10000000000000000000000000000"),
(
"-20165.4676_e-+4294967292",
"ERR:Scale exceeds the maximum precision allowed: 4294967292 > 28",
),
];

for &(value, expected) in tests {
assert_eq!(expected, Decimal::from_scientific(value).unwrap().to_string());
if expected.starts_with("ERR:") {
assert_eq!(&expected[4..], Decimal::from_scientific(value).unwrap_err().to_string(),);
} else {
assert_eq!(expected, Decimal::from_scientific(value).unwrap().to_string());
}
}
}

Expand Down

0 comments on commit 722af9f

Please sign in to comment.