Repository: firecracker-microvm/firecracker
File: src/vmm/src/devices/virtio/vsock/packet.rs (Rust)

Code before:
    /// Returns
    /// - [`VsockError::UnreadableDescriptor`] if the provided descriptor chain contains any
    ///   descriptor not marked as writable.
    /// - [`VsockError::DescChainTooShortForHeader`] if the descriptor chain's total buffer length
    ///   is insufficient to hold the 44 byte vsock header
    /// - [`VsockError::InvalidPktLen`] if the contained vsock header describes a vsock packet whose
    ///   length would exceed [`defs::MAX_PKT_BUR_SIZE`].
    /// - [`VsockError::DescChainTooShortForPacket`] if the contained vsock header describes a vsock
    ///   packet whose length exceeds the descriptor chain's actual total buffer length.
    pub fn from_tx_virtq_head(chain: DescriptorChain) -> Result<Self, VsockError> {
        let buffer = IoVecBuffer::from_descriptor_chain(chain)?;

        let mut hdr = VsockPacketHeader::default();
        match buffer.read_exact_volatile_at(hdr.as_mut_slice(), 0) {
            Ok(()) => (),
            Err(Error::PartialBuffer { completed, .. }) => {
                return Err(VsockError::DescChainTooShortForHeader(completed))
            }
            Err(err) => return Err(VsockError::GuestMemoryMmap(err.into())),
        }

        if hdr.len > defs::MAX_PKT_BUF_SIZE {
            return Err(VsockError::InvalidPktLen(hdr.len));
        }

        if (hdr.len) > buffer.len() - VSOCK_PKT_HDR_SIZE {
            return Err(VsockError::DescChainTooShortForPacket(
                buffer.len(),
                hdr.len,
            ));
        }

        Ok(VsockPacket {
            hdr,
            buffer: VsockPacketBuffer::Tx(buffer),
        })
    }

    /// Create the packet wrapper from an RX virtq chain head.
    ///
    /// ## Errors
    /// Returns [`VsockError::DescChainTooShortForHeader`] if the descriptor chain's total buffer
    /// length is insufficient to hold the 44 byte vsock header
    pub fn from_rx_virtq_head(chain: DescriptorChain) -> Result<Self, VsockError> {
        let buffer = IoVecBufferMut::from_descriptor_chain(chain)?;

        if buffer.len() < VSOCK_PKT_HDR_SIZE {
            return Err(VsockError::DescChainTooShortForHeader(buffer.len() as usize));
        }

        Ok(Self {

Review comment (suggestion):
nit:
```suggestion
        if hdr.len > buffer.len() - VSOCK_PKT_HDR_SIZE {
```

Code after:
    /// Returns
    /// - [`VsockError::UnreadableDescriptor`] if the provided descriptor chain contains any
    ///   descriptor not marked as writable.
    /// - [`VsockError::DescChainTooShortForHeader`] if the descriptor chain's total buffer length
    ///   is insufficient to hold the 44 byte vsock header
    /// - [`VsockError::InvalidPktLen`] if the contained vsock header describes a vsock packet whose
    ///   length would exceed [`defs::MAX_PKT_BUR_SIZE`].
    /// - [`VsockError::DescChainTooShortForPacket`] if the contained vsock header describes a vsock
    ///   packet whose length exceeds the descriptor chain's actual total buffer length.
    pub fn from_tx_virtq_head(chain: DescriptorChain) -> Result<Self, VsockError> {
        let buffer = IoVecBuffer::from_descriptor_chain(chain)?;

        let mut hdr = VsockPacketHeader::default();
        match buffer.read_exact_volatile_at(hdr.as_mut_slice(), 0) {
            Ok(()) => (),
            Err(Error::PartialBuffer { completed, .. }) => {
                return Err(VsockError::DescChainTooShortForHeader(completed))
            }
            Err(err) => return Err(VsockError::GuestMemoryMmap(err.into())),
        }

        if hdr.len > defs::MAX_PKT_BUF_SIZE {
            return Err(VsockError::InvalidPktLen(hdr.len));
        }

        if hdr.len > buffer.len() - VSOCK_PKT_HDR_SIZE {
            return Err(VsockError::DescChainTooShortForPacket(
                buffer.len(),
                hdr.len,
            ));
        }

        Ok(VsockPacket {
            hdr,
            buffer: VsockPacketBuffer::Tx(buffer),
        })
    }

    /// Create the packet wrapper from an RX virtq chain head.
    ///
    /// ## Errors
    /// Returns [`VsockError::DescChainTooShortForHeader`] if the descriptor chain's total buffer
    /// length is insufficient to hold the 44 byte vsock header
    pub fn from_rx_virtq_head(chain: DescriptorChain) -> Result<Self, VsockError> {
        let buffer = IoVecBufferMut::from_descriptor_chain(chain)?;

        if buffer.len() < VSOCK_PKT_HDR_SIZE {
            return Err(VsockError::DescChainTooShortForHeader(buffer.len() as usize));
        }

        Ok(Self {