Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ jobs:
- name: miri
if: matrix.toolchain == 'nightly'
run: bash ./scripts/run_miri.sh
env:
MIRIFLAGS: '-Zmiri-tag-raw-pointers'

- name: fuzz
if: env.DO_FUZZ == '1'
Expand Down
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,11 @@ impl<'a, T: 'a + Array> Drop for Drain<'a, T> {
let start = source_vec.len();
let tail = self.tail_start;
if tail != start {
let src = source_vec.as_ptr().add(tail);
let dst = source_vec.as_mut_ptr().add(start);
// as_mut_ptr creates a &mut, invalidating other pointers.
// This pattern avoids calling it with a pointer already present.
let ptr = source_vec.as_mut_ptr();
let src = ptr.add(tail);
let dst = ptr.add(start);
ptr::copy(src, dst, self.tail_len);
}
source_vec.set_len(start + self.tail_len);
Expand Down Expand Up @@ -813,13 +816,14 @@ impl<A: Array> SmallVec<A> {
unsafe {
self.set_len(start);

let range_slice = slice::from_raw_parts_mut(self.as_mut_ptr().add(start), end - start);
let range_slice = slice::from_raw_parts(self.as_ptr().add(start), end - start);

Drain {
tail_start: end,
tail_len: len - end,
iter: range_slice.iter(),
vec: NonNull::from(self),
// Since self is a &mut, passing it to a function would invalidate the slice iterator.
vec: NonNull::new_unchecked(self as *mut _),
}
}
}
Expand Down Expand Up @@ -1112,6 +1116,10 @@ impl<A: Array> SmallVec<A> {
len: old_len + lower_size_bound,
};

// The set_len above invalidates the previous pointers, so we must re-create them.
let start = self.as_mut_ptr();
let ptr = start.add(index);

while num_added < lower_size_bound {
let element = match iter.next() {
Some(x) => x,
Expand Down