Skip to content

Commit

Permalink
Rollup merge of rust-lang#78476 - RalfJung:btree-alias, r=Mark-Simula…
Browse files Browse the repository at this point in the history
…crum

fix some incorrect aliasing in the BTree

This line is wrong:
```
ptr::copy(slice.as_ptr().add(idx), slice.as_mut_ptr().add(idx + 1), slice.len() - idx);
```
When `slice.as_mut_ptr()` is called, that creates a mutable reference to the entire slice, which invalidates the raw pointer previously returned by `slice.as_ptr()`. (Miri currently misses this because raw pointers are not tracked properly.)

Cc `````@ssomers`````
  • Loading branch information
m-ou-se authored Nov 7, 2020
2 parents 10ea17b + b0df3f7 commit 61cd0ea
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions library/alloc/src/collections/btree/node.rs
Original file line number Diff line number Diff line change
@@ -1608,15 +1608,19 @@ pub mod marker {

unsafe fn slice_insert<T>(slice: &mut [T], idx: usize, val: T) {
unsafe {
ptr::copy(slice.as_ptr().add(idx), slice.as_mut_ptr().add(idx + 1), slice.len() - idx);
ptr::write(slice.get_unchecked_mut(idx), val);
let len = slice.len();
let slice_ptr = slice.as_mut_ptr();
ptr::copy(slice_ptr.add(idx), slice_ptr.add(idx + 1), len - idx);
ptr::write(slice_ptr.add(idx), val);
}
}

unsafe fn slice_remove<T>(slice: &mut [T], idx: usize) -> T {
unsafe {
let ret = ptr::read(slice.get_unchecked(idx));
ptr::copy(slice.as_ptr().add(idx + 1), slice.as_mut_ptr().add(idx), slice.len() - idx - 1);
let len = slice.len();
let slice_ptr = slice.as_mut_ptr();
let ret = ptr::read(slice_ptr.add(idx));
ptr::copy(slice_ptr.add(idx + 1), slice_ptr.add(idx), len - idx - 1);
ret
}
}

0 comments on commit 61cd0ea

Please sign in to comment.