From ea24395617745dd0483a7c4114da04b114a7d3d8 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 20 Oct 2020 20:20:06 +0200 Subject: [PATCH] Add debug_asserts for the unsafe indexing in fmt::write. --- library/core/src/fmt/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 04edf4611ecc..cc84bf14a33a 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -1110,7 +1110,7 @@ unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument, args: &[ArgumentV } // Extract the correct argument - + debug_assert!(arg.position < args.len()); // SAFETY: arg and args come from the same Arguments, // which guarantees its index is always within bounds. let value = unsafe { args.get_unchecked(arg.position) }; @@ -1124,6 +1124,7 @@ unsafe fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::v1::Count) -> Option Some(n), rt::v1::Count::Implied => None, rt::v1::Count::Param(i) => { + debug_assert!(i < args.len()); // SAFETY: cnt and args come from the same Arguments, // which guarantees this index is always within bounds. unsafe { args.get_unchecked(i).as_usize() }