fix[sqlx-postgres]: do a checked_mul to prevent panic when casting from db (#3919)

This commit is contained in:
Nicolás Hatcher 2025-07-05 01:51:43 +02:00 committed by GitHub
parent 29549b14c4
commit 9de593a0e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -62,7 +62,9 @@ impl TryFrom<&'_ PgNumeric> for Decimal {
.checked_powi(weight as i64)
.ok_or("value not representable as rust_decimal::Decimal")?;
let part = Decimal::from(digit) * mul;
let part = Decimal::from(digit)
.checked_mul(mul)
.ok_or("value not representable as rust_decimal::Decimal")?;
value = value
.checked_add(part)
@ -383,6 +385,19 @@ mod tests {
assert_eq!(actual_decimal.scale(), 0);
}
#[test]
fn mult_overflow() {
// -24_0711_6702_1036_7100_2022_8579_3280.00
let large_negative_number = PgNumeric::Number {
sign: PgNumericSign::Negative,
scale: 0,
weight: 7,
digits: vec![24, 0711, 6702, 1036, 7100, 2022, 8579, 3280],
};
assert!(Decimal::try_from(large_negative_number).is_err());
}
#[test]
fn max_value_max_scale() {
let mut max_value_max_scale = Decimal::MAX;