Skip to content

Commit 14f4ffa

Browse files
committed
Use spare_capacity_mut instead of invalid unchecked indexing
1 parent e6b883c commit 14f4ffa

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Diff for: library/alloc/src/str.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,20 @@ where
178178

179179
unsafe {
180180
let pos = result.len();
181-
let target = result.get_unchecked_mut(pos..reserved_len);
181+
let target = result.spare_capacity_mut().get_unchecked_mut(..reserved_len - pos);
182+
183+
// Convert the separator and slices to slices of MaybeUninit
184+
// to simplify implementation in specialize_for_lengths
185+
let sep_uninit = core::slice::from_raw_parts(sep.as_ptr().cast(), sep.len());
186+
let iter_uninit = iter.map(|it| {
187+
let it = it.borrow().as_ref();
188+
core::slice::from_raw_parts(it.as_ptr().cast(), it.len())
189+
});
182190

183191
// copy separator and slices over without bounds checks
184192
// generate loops with hardcoded offsets for small separators
185193
// massive improvements possible (~ x2)
186-
let remain = specialize_for_lengths!(sep, target, iter; 0, 1, 2, 3, 4);
194+
let remain = specialize_for_lengths!(sep_uninit, target, iter_uninit; 0, 1, 2, 3, 4);
187195

188196
// A weird borrow implementation may return different
189197
// slices for the length calculation and the actual copy.

0 commit comments

Comments
 (0)