Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c12f4d1

Browse files
authoredNov 5, 2024
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.
2 parents bbb9275 + 2ae24bf commit c12f4d1

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed
 

‎core/src/fmt/num.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ unsafe trait GenericRadix: Sized {
8888
};
8989
}
9090
}
91-
let buf = &buf[curr..];
91+
// SAFETY: `curr` is initialized to `buf.len()` and is only decremented, so it can't overflow. It is
92+
// decremented exactly once for each digit. Since u128 is the widest fixed width integer format supported,
93+
// the maximum number of digits (bits) is 128 for base-2, so `curr` won't underflow as well.
94+
let buf = unsafe { buf.get_unchecked(curr..) };
9295
// SAFETY: The only chars in `buf` are created by `Self::digit` which are assumed to be
9396
// valid UTF-8
9497
let buf = unsafe {

0 commit comments

Comments
 (0)
Failed to load comments.