Skip to content

Panic when parsing byte unit with very long fractional part #5

Closed
@5225225

Description

@5225225
assert_parses! {
    "9.00000000000000000000MB" => 9.megabytes(),
}

This test should pass, but instead you get

thread 'parse::parse_tests::accept' panicked at 'attempt to multiply with overflow', /rustc/8b09ba6a5d5c644fe0f1c27c7f9c80b334241707/library/core/src/num/mod.rs:843:5
stack backtrace:
   0: rust_begin_unwind
             at /rustc/8b09ba6a5d5c644fe0f1c27c7f9c80b334241707/library/std/src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/8b09ba6a5d5c644fe0f1c27c7f9c80b334241707/library/core/src/panicking.rs:106:14
   2: core::panicking::panic
             at /rustc/8b09ba6a5d5c644fe0f1c27c7f9c80b334241707/library/core/src/panicking.rs:47:5
   3: core::num::<impl u64>::pow
             at /rustc/8b09ba6a5d5c644fe0f1c27c7f9c80b334241707/library/core/src/num/uint_macros.rs:1955:13
   4: ubyte::parse::<impl core::str::traits::FromStr for ubyte::byte_unit::ByteUnit>::from_str
             at ./src/parse.rs:77:47
   5: ubyte::parse::parse_tests::accept
             at ./src/parse.rs:183:9
   6: ubyte::parse::parse_tests::accept::{{closure}}
             at ./src/parse.rs:134:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/8b09ba6a5d5c644fe0f1c27c7f9c80b334241707/library/core/src/ops/function.rs:227:5
   8: core::ops::function::FnOnce::call_once
             at /rustc/8b09ba6a5d5c644fe0f1c27c7f9c80b334241707/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

The problem is the .pow in

let frac_part = frac as f64 / 10u64.pow(frac_str.len() as u32) as f64;

I think this probably should be parsed successfully (as opposed to giving an error).

I'll have a look at trying to fix the arithmetic, but this kind of code is kinda hard to reason about for me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions