about summary refs log tree commit diff
path: root/compiler/rustc_middle/src
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-07-19 11:37:49 +0200
committerGitHub <noreply@github.com>2021-07-19 11:37:49 +0200
commit6cb69ea790ad635acb3a8d4826e03ac29ca10351 (patch)
tree8929ee5e23a7bd91146213570268566f37deb884 /compiler/rustc_middle/src
parent5ef6439c8d4df4f54cfdb96e7ff66ebd062e26e9 (diff)
parent98e9d16d258154c319d7e48159ad2f65c3244713 (diff)
downloadrust-6cb69ea790ad635acb3a8d4826e03ac29ca10351.tar.gz
rust-6cb69ea790ad635acb3a8d4826e03ac29ca10351.zip
Rollup merge of #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.
Diffstat (limited to 'compiler/rustc_middle/src')
-rw-r--r--compiler/rustc_middle/src/ty/list.rs12
1 files 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<T: Copy> List<T> {
 
         let (layout, _offset) =
             Layout::new::<usize>().extend(Layout::for_value::<[T]>(slice)).unwrap();
-        let mem = arena.dropless.alloc_raw(layout);
+        let mem = arena.dropless.alloc_raw(layout) as *mut List<T>;
         unsafe {
-            let result = &mut *(mem as *mut List<T>);
             // 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::<T>()
+                .copy_from_nonoverlapping(slice.as_ptr(), slice.len());
 
-            result
+            &mut *mem
         }
     }