Skip to content

f64 formatting panic with 'arithmetic operation overflowed' #23150

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

Closed
Gekkio opened this issue Mar 7, 2015 · 4 comments
Closed

f64 formatting panic with 'arithmetic operation overflowed' #23150

Gekkio opened this issue Mar 7, 2015 · 4 comments
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@Gekkio
Copy link
Contributor

Gekkio commented Mar 7, 2015

Here's a minimal example which results in a runtime error:

fn main() {
    println!("{:.0}", 9.9);
}

I'd expect that format string to work (precision=0, so no digits after the decimal point), but here's what I get with RUST_BACKTRACE=1:

thread '<main>' panicked at 'arithmetic operation overflowed', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libcore/fmt/float.rs:245
stack backtrace:
   1:     0x7f92f7b9381f - sys::backtrace::write::h2f046b875b210aadOBA
   2:     0x7f92f7b97db2 - panicking::on_panic::h792ac47fb8c07faehHJ
   3:     0x7f92f7b8dd6a - rt::unwind::begin_unwind_inner::hbffac672008352c8knJ
   4:     0x7f92f7b8e481 - rt::unwind::begin_unwind_fmt::hbe64affaa986f482VlJ
   5:     0x7f92f7b976d7 - rust_begin_unwind
   6:     0x7f92f7bc1f64 - panicking::panic_fmt::h81cd96819f02da8dIZs
   7:     0x7f92f7bc1e8d - panicking::panic::h8ea414638b29b322UXs
   8:     0x7f92f7bc6066 - fmt::f64.Display::fmt::hb0a5033b26a63c76UzD
   9:     0x7f92f7bc5071 - fmt::write::h3770d88fab3d43d6YXC
  10:     0x7f92f7bc4d78 - fmt::Arguments<'a>.Debug::fmt::h542db8d18a17ace0xVC
  11:     0x7f92f7bc5110 - fmt::write::h3770d88fab3d43d6YXC
  12:     0x7f92f7b9130c - old_io::Writer::write_fmt::h7179537900110901984
  13:     0x7f92f7b92275 - old_io::stdio::println_args::hee7144bbc1e28d79dNg
  14:     0x7f92f7b8d6e8 - main::h35109694ee10d661eaa
  15:     0x7f92f7b9c1c8 - rust_try_inner
  16:     0x7f92f7b9c1b5 - rust_try
  17:     0x7f92f7b9978c - rt::lang_start::h3d4a7f7be4f6d99bxBJ
  18:     0x7f92f7b8d74e - main
  19:     0x7f92f6d7dec4 - __libc_start_main
  20:     0x7f92f7b8d4f8 - <unknown>
  21:                0x0 - <unknown>

Ubuntu 14.10 / 64-bit
rustc 1.0.0-nightly (b0746ff 2015-03-05) (built 2015-03-06)
binary: rustc
commit-hash: b0746ff
commit-date: 2015-03-05
build-date: 2015-03-06
host: x86_64-unknown-linux-gnu
release: 1.0.0-nightly

@lifthrasiir
Copy link
Contributor

It also happens with 9.5 but not with 9.499. The formatting routine tries to round, but fails to handle the case that rounding extends the digits.

@steveklabnik steveklabnik added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Mar 8, 2015
@Gekkio
Copy link
Contributor Author

Gekkio commented Mar 9, 2015

Just to clarify: this is not actually an ICE because the program is panicking, not the compiler. So, compilation is fine, but the program fails at runtime

benashford added a commit to benashford/rust that referenced this issue Apr 10, 2015
Manishearth added a commit to Manishearth/rust that referenced this issue Apr 11, 2015
…ichton

 This fixes the bug described in issue rust-lang#23150.  This affected formatting any floating point number into a string in a formatting pattern that: a) required rounding up, and b) required an extra digit on the front.

So `format!(\"{:.0}\", 9.9)` would fail, but `format!(\"{:.0}\", 8.9)` would succeed.  This was due to a negative integer being cast to a `usize` resulting in an 'arithmetic operation overflowed' panic.

The fix was to change the order of operations so that the number is zero before casting.
bors added a commit that referenced this issue Apr 11, 2015
This fixes the bug described in issue #23150.  This affected formatting any floating point number into a string in a formatting pattern that: a) required rounding up, and b) required an extra digit on the front.

So `format!("{:.0}", 9.9)` would fail, but `format!("{:.0}", 8.9)` would succeed.  This was due to a negative integer being cast to a `usize` resulting in an 'arithmetic operation overflowed' panic.

The fix was to change the order of operations so that the number is zero before casting.
@lifthrasiir
Copy link
Contributor

This issue should be closed as it's now fixed.

@alexcrichton
Copy link
Member

Yay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

4 participants