Skip to content

Commit 6cb69ea

Browse files
Rollup merge of rust-lang#87268 - SkiFire13:fix-uninit-ref-list, r=nagisa
Don't create references to uninitialized data in `List::from_arena` Previously `result` and `arena_slice` were references pointing to uninitialized data, which is technically UB. They may have been fine because the pointed data is `Copy` and and they were only written to, but the semantics of this aren't clearly defined yet, and since we have a sound way to do the same thing I don't think we should keep the possibly-unsound way.
2 parents 5ef6439 + 98e9d16 commit 6cb69ea

File tree

1 file changed

+6
-6
lines changed
  • compiler/rustc_middle/src/ty

1 file changed

+6
-6
lines changed

compiler/rustc_middle/src/ty/list.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,17 @@ impl<T: Copy> List<T> {
6363

6464
let (layout, _offset) =
6565
Layout::new::<usize>().extend(Layout::for_value::<[T]>(slice)).unwrap();
66-
let mem = arena.dropless.alloc_raw(layout);
66+
let mem = arena.dropless.alloc_raw(layout) as *mut List<T>;
6767
unsafe {
68-
let result = &mut *(mem as *mut List<T>);
6968
// Write the length
70-
result.len = slice.len();
69+
ptr::addr_of_mut!((*mem).len).write(slice.len());
7170

7271
// Write the elements
73-
let arena_slice = slice::from_raw_parts_mut(result.data.as_mut_ptr(), result.len);
74-
arena_slice.copy_from_slice(slice);
72+
ptr::addr_of_mut!((*mem).data)
73+
.cast::<T>()
74+
.copy_from_nonoverlapping(slice.as_ptr(), slice.len());
7575

76-
result
76+
&mut *mem
7777
}
7878
}
7979

0 commit comments

Comments
 (0)