From 581aa8d587f47d1eece784f20bd18ad736bfe091 Mon Sep 17 00:00:00 2001 From: Eugene Shamis Date: Fri, 1 Nov 2024 15:33:07 -0400 Subject: [PATCH] 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. --- core/src/fmt/num.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/src/fmt/num.rs b/core/src/fmt/num.rs index f1540803f978d..aaf429bac8e7f 100644 --- a/core/src/fmt/num.rs +++ b/core/src/fmt/num.rs @@ -88,7 +88,9 @@ unsafe trait GenericRadix: Sized { }; } } - let buf = &buf[curr..]; + // SAFETY: `curr` is initialized to `buf.len()` and is only decremented, + // so it is always in bounds. + 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 {