-
Notifications
You must be signed in to change notification settings - Fork 13k
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
UpperHex formatting might panic #126425
Comments
I suppose a |
It's OK that it won't panic in practice, but the compiler is unable to prove it, so it is generating the panic code and linking with panic-related functionality which is desirable to avoid. That is, using crates such as no_panics_whatsoever or similar functionality by other means won't work. |
@rustbot label -needs-triage +T-libs +A-panic +C-enhancement |
Fixes rust-lang#126425 Replace the potentially panicking `[]` indexing with `get_unchecked()` to prevent linking with panic-related code.
Based on minimizing the code to: pub fn fmt_int(buf: &mut [u8; 128], mut x: u128) -> &[u8] {
let mut curr = buf.len();
for byte in buf.iter_mut().rev() {
if *byte == 0 { break; }
curr -= 1;
}
&buf[curr..]
} it seems like LLVM should be removing this panic: https://alive2.llvm.org/ce/z/UJHdEr |
/might/ be related to #127553 |
I think in the original code the compiler might not be able to infer that |
…joboet [core/fmt] Replace checked slice indexing by unchecked to support panic-free code Fixes rust-lang#126425 Replace the potentially panicking `[]` indexing with `get_unchecked()` to prevent linking with panic-related code.
…joboet [core/fmt] Replace checked slice indexing by unchecked to support panic-free code Fixes rust-lang#126425 Replace the potentially panicking `[]` indexing with `get_unchecked()` to prevent linking with panic-related code.
…joboet [core/fmt] Replace checked slice indexing by unchecked to support panic-free code Fixes rust-lang#126425 Replace the potentially panicking `[]` indexing with `get_unchecked()` to prevent linking with panic-related code.
Rollup merge of rust-lang#132473 - ZhekaS:core_fmt_radix_no_panic, r=joboet [core/fmt] Replace checked slice indexing by unchecked to support panic-free code Fixes rust-lang#126425 Replace the potentially panicking `[]` indexing with `get_unchecked()` to prevent linking with panic-related code.
When writing non-panicking code, it is impossible to use the
"{:X}"
format specifier as theimpl core::fmt::UpperHex for usize
and similar code might panic. It seems that it is because the GenericRadix::fmt_int method is using slice indexing notation here:This seem to be easily avoidable by replacing it with
buf.get(curr..)
and some error handling.The text was updated successfully, but these errors were encountered: