diff options
| author | Jubilee <workingjubilee@gmail.com> | 2024-11-05 01:34:23 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-05 01:34:23 -0800 |
| commit | 57f64c67e098b1e4211c7c5ba441a8a60f4f789d (patch) | |
| tree | 47ef57e0f8d5b9856e6b6a63b61397c773c3c25e | |
| parent | 1ee661741408ad7d4872bc668541fd4a4616b580 (diff) | |
| parent | 65d8f1b8bfe79a3b88048ce24629456b007f8f4a (diff) | |
| download | rust-57f64c67e098b1e4211c7c5ba441a8a60f4f789d.tar.gz rust-57f64c67e098b1e4211c7c5ba441a8a60f4f789d.zip | |
Rollup merge of #132473 - ZhekaS:core_fmt_radix_no_panic, r=joboet
[core/fmt] Replace checked slice indexing by unchecked to support panic-free code Fixes #126425 Replace the potentially panicking `[]` indexing with `get_unchecked()` to prevent linking with panic-related code.
| -rw-r--r-- | library/core/src/fmt/num.rs | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/library/core/src/fmt/num.rs b/library/core/src/fmt/num.rs index f1540803f97..d43f25d9fd1 100644 --- a/library/core/src/fmt/num.rs +++ b/library/core/src/fmt/num.rs @@ -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 { |
