Skip to content

Commit

Permalink
Rollup merge of rust-lang#67003 - cjgillot:corrida, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Fix TypedArena returning wrong pointers for recursive allocations

Closes rust-lang#67001
  • Loading branch information
Centril committed Dec 8, 2019
2 parents f038de2 + d84eb50 commit 9384dd7
Showing 1 changed file with 12 additions and 47 deletions.
59 changes: 12 additions & 47 deletions src/libarena/lib.rs
Expand Up @@ -202,53 +202,18 @@ impl<T> TypedArena<T> {
#[inline]
pub fn alloc_from_iter<I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] {
assert!(mem::size_of::<T>() != 0);
let mut iter = iter.into_iter();
let size_hint = iter.size_hint();

match size_hint {
(min, Some(max)) if min == max => {
// We know the exact number of elements the iterator will produce here
let len = min;

if len == 0 {
return &mut [];
}

self.ensure_capacity(len);

let slice = self.ptr.get();

unsafe {
let mut ptr = self.ptr.get();
for _ in 0..len {
// Write into uninitialized memory.
ptr::write(ptr, iter.next().unwrap());
// Advance the pointer.
ptr = ptr.offset(1);
// Update the pointer per iteration so if `iter.next()` panics
// we destroy the correct amount
self.ptr.set(ptr);
}
slice::from_raw_parts_mut(slice, len)
}
}
_ => {
cold_path(move || -> &mut [T] {
let mut vec: SmallVec<[_; 8]> = iter.collect();
if vec.is_empty() {
return &mut [];
}
// Move the content to the arena by copying it and then forgetting
// the content of the SmallVec
unsafe {
let len = vec.len();
let start_ptr = self.alloc_raw_slice(len);
vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
vec.set_len(0);
slice::from_raw_parts_mut(start_ptr, len)
}
})
}
let mut vec: SmallVec<[_; 8]> = iter.into_iter().collect();
if vec.is_empty() {
return &mut [];
}
// Move the content to the arena by copying it and then forgetting
// the content of the SmallVec
unsafe {
let len = vec.len();
let start_ptr = self.alloc_raw_slice(len);
vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
vec.set_len(0);
slice::from_raw_parts_mut(start_ptr, len)
}
}

Expand Down

0 comments on commit 9384dd7

Please sign in to comment.