diff --git a/virtio-devices/src/vsock/csm/txbuf.rs b/virtio-devices/src/vsock/csm/txbuf.rs index 8ed1cdd4b..c4e4f6728 100644 --- a/virtio-devices/src/vsock/csm/txbuf.rs +++ b/virtio-devices/src/vsock/csm/txbuf.rs @@ -3,7 +3,6 @@ // use std::io::Write; -use std::mem; use std::num::Wrapping; use super::defs; @@ -15,7 +14,7 @@ use super::{Error, Result}; /// pub struct TxBuf { /// The actual u8 buffer - only allocated after the first push. - data: Option>, + data: Option>, /// Ring-buffer head offset - where new data is pushed to. head: Wrapping, /// Ring-buffer tail offset - where data is flushed from. @@ -55,18 +54,9 @@ impl TxBuf { return Err(Error::TxBufFull); } - // We're using a closure here to return the boxed slice, instead of a value (i.e. - // `get_or_insert_with()` instead of `get_or_insert()`), because we only want the box - // created when `self.data` is None. If we were to use `get_or_insert(box)`, the box - // argument would always get evaluated (which implies a heap allocation), even though - // it would later be discarded (when `self.data.is_some()`). Apparently, clippy fails - // to see this, and insists on issuing some warning. - #[allow(clippy::redundant_closure)] - let data = self.data.get_or_insert_with(|| - // Using uninitialized memory here is quite safe, since we never read from any - // area of the buffer before writing to it. First we push, then we flush only - // what had been previously pushed. - Box::new(unsafe {mem::MaybeUninit::<[u8; Self::SIZE]>::uninit().assume_init()})); + let data = self + .data + .get_or_insert_with(|| vec![0u8; Self::SIZE].into_boxed_slice()); // Buffer head, as an offset into the data slice. let head_ofs = self.head.0 as usize % Self::SIZE;