about summary refs log tree commit diff
path: root/compiler/rustc_serialize/src/opaque/mem_encoder.rs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_serialize/src/opaque/mem_encoder.rs')
-rw-r--r--compiler/rustc_serialize/src/opaque/mem_encoder.rs11
1 files changed, 9 insertions, 2 deletions
diff --git a/compiler/rustc_serialize/src/opaque/mem_encoder.rs b/compiler/rustc_serialize/src/opaque/mem_encoder.rs
index 5104df04215..56beebb49af 100644
--- a/compiler/rustc_serialize/src/opaque/mem_encoder.rs
+++ b/compiler/rustc_serialize/src/opaque/mem_encoder.rs
@@ -35,8 +35,15 @@ impl MemEncoder {
         self.data.reserve(N);
 
         let old_len = self.data.len();
-        // SAFETY: fix
-        let buf = unsafe { &mut *(self.data.as_mut_ptr().add(old_len) as *mut [u8; N]) };
+
+        // SAFETY: The above `reserve` ensures that there is enough
+        // room to write the encoded value to the vector's internal buffer.
+        // The memory is also initialized as 0.
+        let buf = unsafe {
+            let buf = self.data.as_mut_ptr().add(old_len) as *mut [u8; N];
+            *buf = [0; N];
+            &mut *buf
+        };
         let written = visitor(buf);
         if written > N {
             Self::panic_invalid_write::<N>(written);