Skip to content

Commit

Permalink
Rollup merge of rust-lang#132473 - ZhekaS:core_fmt_radix_no_panic, r=…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
workingjubilee authored Nov 4, 2024
2 parents 65a3146 + 65d8f1b commit 9f1dcd5
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion library/core/src/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ unsafe trait GenericRadix: Sized {
};
}
}
let buf = &buf[curr..];
// SAFETY: `curr` is initialized to `buf.len()` and is only decremented, so it can't overflow. It is
// decremented exactly once for each digit. Since u128 is the widest fixed width integer format supported,
// the maximum number of digits (bits) is 128 for base-2, so `curr` won't underflow as well.
let buf = unsafe { buf.get_unchecked(curr..) };
// SAFETY: The only chars in `buf` are created by `Self::digit` which are assumed to be
// valid UTF-8
let buf = unsafe {
Expand Down

0 comments on commit 9f1dcd5

Please sign in to comment.