Pumpkin-MC/Pumpkin · error

expected i16

Error message

expected i16

What it means

Thrown by `PacketReadSlice for i16` when fewer than 2 bytes remain in the buffer. The deserializer needs exactly 2 bytes to reconstruct a little-endian i16. It is an UnexpectedEof error from premature buffer exhaustion.

Solutions

  1. Ensure the packet contains at least 2 bytes for this field before decoding
  2. Check that prior VarUInt fields parsed the correct number of bytes
  3. Validate total payload length against the header
  4. Handle and reject the malformed packet

Example fix

// before
let temp = i16::read_slice(&mut buf)?;
// after
if buf.len() < 2 { return Err(PacketError::Truncated); }
let temp = i16::read_slice(&mut buf)?;
Defensive patterns

Strategy: try-catch

Validate before calling

if buf.len() < 2 { return Err(PacketError::Truncated); }

Try / catch

let v = i16::read_slice(&mut buf)
    .map_err(|e| { log::debug!("short read: {e}"); PacketError::Truncated })?;

Prevention

When it happens

Trigger: i16::read_slice called when buf.len() < 2 — packet payload shorter than the struct layout being decoded.

Common situations: Truncated packet body; missing padding before an i16 field; field misalignment from a previous VarInt of wrong length.

Related errors


AI-assisted analysis of Pumpkin-MC/Pumpkin@8d4639e25a (2026-09-09). Data as JSON: /api/errors/8af4ae903b67e0a7. Report an issue: GitHub.

Appendix: source

Thrown at crates/pumpkin-protocol/src/serial/deserializer.rs:297

        if buf.is_empty() {
            return Err(Error::new(ErrorKind::UnexpectedEof, "expected u8"));
        }
        let b = buf[0];
        *buf = &buf[1..];
        Ok(b)
    }
}

impl<'a> PacketReadSlice<'a> for i8 {
    fn read_slice(buf: &mut &'a [u8]) -> Result<Self, Error> {
        u8::read_slice(buf).map(|b| b as Self)
    }
}

impl<'a> PacketReadSlice<'a> for i16 {
    fn read_slice(buf: &mut &'a [u8]) -> Result<Self, Error> {
        if buf.len() < 2 {
            return Err(Error::new(ErrorKind::UnexpectedEof, "expected i16"));
        }
        let (bytes, rest) = buf.split_at(2);
        *buf = rest;
        let arr = bytes
            .try_into()
            .map_err(|_| Error::new(ErrorKind::InvalidData, "invalid i16 slice"))?;
        Ok(Self::from_le_bytes(arr))
    }
}

impl<'a> PacketReadSlice<'a> for i32 {
    fn read_slice(buf: &mut &'a [u8]) -> Result<Self, Error> {
        if buf.len() < 4 {
            return Err(Error::new(ErrorKind::UnexpectedEof, "expected i32"));
        }
        let (bytes, rest) = buf.split_at(4);
        *buf = rest;
        let arr = bytes

View on GitHub (pinned to 8d4639e25a)