How can I resolve a server crash due to a 'Multiplication overflowed' panic in Rust?

I'm encountering a server crash with a panic related to 'Multiplication overflowed' in Rust. Here's the relevant part of the log:

thread 'rw-streaming' panicked at /root/.cargo/registry/src/
Multiplication overflowed

It seems to happen when converting from a Postgres row to a Rust struct. I'm not sure if it's related to handling large decimal numbers or something else. How can I resolve this issue?



Asked on Mar 26, 2024

The panic indicates that there's an overflow when performing a multiplication operation on decimal numbers. This can happen if the numbers involved are too large to be represented within the limits of the rust_decimal type. To resolve this issue, you can try the following steps:

  1. Check if the decimal numbers involved in the multiplication are within the range that rust_decimal can handle.
  2. If the numbers are too large, consider using a different data type that can handle larger numbers, such as BigInt or a custom type for arbitrary precision arithmetic.
  3. If the overflow is expected and can be handled in your application logic, you can catch the panic using std::panic::catch_unwind and deal with the overflow case accordingly.
  4. If the overflow is due to a bug in the code, such as incorrect logic or assumptions about the input data, fix the underlying issue.
  5. If the issue is with the rust_decimal crate itself, consider reporting it to the maintainers or looking for an alternative crate that better suits your needs.

Here's an example of how you might handle the overflow in code:

use rust_decimal::Decimal;
use std::panic::{self, AssertUnwindSafe};

fn main() {
    let result = panic::catch_unwind(AssertUnwindSafe(|| {
        // Your multiplication operation that might overflow
        let a = Decimal::new(1, 0);
        let b = Decimal::new(1, 0);
        a * b

    match result {
        Ok(value) => println!("Result: {}", value),
        Err(_) => println!("Multiplication overflowed"),

This code attempts to catch the panic caused by an overflow during multiplication and prints an error message instead of crashing the server.

Mar 26, 2024Edited by