paupino / rust-decimal

Decimal number implementation written in pure Rust suitable for financial and fixed-precision calculations.
https://docs.rs/rust_decimal/
MIT License
972 stars 177 forks source link

Return error rather than panic in `FromSql` when overflow #652

Closed xiangjinwu closed 5 months ago

xiangjinwu commented 5 months ago

Get binary encoding from PostgreSQL:

test=# select numeric_send(1e28);
      numeric_send      
------------------------
 \x00010007000000000001
(1 row)

test=# select numeric_send(1e29);
      numeric_send      
------------------------
 \x0001000700000000000a
(1 row)

Call FromSql on Decimal:

    println!(
        "{:?}",
        Decimal::from_sql(
            &Type::NUMERIC,
            &[0x00, 0x01, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]
        )
    );
    println!(
        "{:?}",
        Decimal::from_sql(
            &Type::NUMERIC,
            &[0x00, 0x01, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a]
        )
    );

Expected:

Ok(100000000000000000000)
Err(overflow error)

Actual:

Ok(100000000000000000000)
thread 'main' panicked at rust_decimal-1.34.3/src/arithmetic_impls.rs:232:18:
Multiplication overflowed
paupino commented 5 months ago

Thank you for raising this! It is fortunately an easy issue to fix - I'll see if I can get something out shortly.