From 98e9d16d258154c319d7e48159ad2f65c3244713 Mon Sep 17 00:00:00 2001 From: Giacomo Stevanato Date: Mon, 19 Jul 2021 10:47:45 +0200 Subject: [PATCH] Don't create references to uninitialized data in List::from_arena --- compiler/rustc_middle/src/ty/list.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_middle/src/ty/list.rs b/compiler/rustc_middle/src/ty/list.rs index 44dfcbf1866..1dceda6c7aa 100644 --- a/compiler/rustc_middle/src/ty/list.rs +++ b/compiler/rustc_middle/src/ty/list.rs @@ -63,17 +63,17 @@ impl List { let (layout, _offset) = Layout::new::().extend(Layout::for_value::<[T]>(slice)).unwrap(); - let mem = arena.dropless.alloc_raw(layout); + let mem = arena.dropless.alloc_raw(layout) as *mut List; unsafe { - let result = &mut *(mem as *mut List); // Write the length - result.len = slice.len(); + ptr::addr_of_mut!((*mem).len).write(slice.len()); // Write the elements - let arena_slice = slice::from_raw_parts_mut(result.data.as_mut_ptr(), result.len); - arena_slice.copy_from_slice(slice); + ptr::addr_of_mut!((*mem).data) + .cast::() + .copy_from_nonoverlapping(slice.as_ptr(), slice.len()); - result + &mut *mem } }