Skip to content

Commit

Permalink
Add debug_asserts for the unsafe indexing in fmt::write.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Oct 20, 2020
1 parent d80f127 commit ea24395
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
Expand All @@ -1124,6 +1124,7 @@ unsafe fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::v1::Count) -> Option<usize
rt::v1::Count::Is(n) => 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() }
Expand Down

0 comments on commit ea24395

Please sign in to comment.