Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5225225 opened this issue Nov 10, 2021 · 1 comment
Closed

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

5225225 opened this issue Nov 10, 2021 · 1 comment

Comments

@5225225
Copy link
Contributor

5225225 commented Nov 10, 2021

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.

@SergioBenitez
Copy link
Owner

This is the pow call panicking. The rustdocs make no mention of the call panicking under any conditions: https://doc.rust-lang.org/nightly/std/primitive.u64.html#method.pow. Still, there's clearly the potential for overflow here; I just wish it had been documented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants