Skip to content

Commit c8e7c62

Browse files
committed
Zero the buffer passed from write_with
1 parent 9a6e8b3 commit c8e7c62

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

compiler/rustc_serialize/src/opaque/mem_encoder.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ impl MemEncoder {
3535
self.data.reserve(N);
3636

3737
let old_len = self.data.len();
38-
// SAFETY: fix
39-
let buf = unsafe { &mut *(self.data.as_mut_ptr().add(old_len) as *mut [u8; N]) };
38+
39+
// SAFETY: The above `reserve` ensures that there is enough
40+
// room to write the encoded value to the vector's internal buffer.
41+
// The memory is also initialized as 0.
42+
let buf = unsafe {
43+
let buf = self.data.as_mut_ptr().add(old_len) as *mut [u8; N];
44+
*buf = [0; N];
45+
&mut *buf
46+
};
4047
let written = visitor(buf);
4148
if written > N {
4249
Self::panic_invalid_write::<N>(written);

0 commit comments

Comments
 (0)