Pumpkin-MC/Pumpkin · error · std::io::Error
durability correction must fit in an i16
Error message
durability correction must fit in an i16
What it means
Raised by ItemStackResponseSlotInfo::write when durability_correction (a VarInt) falls outside the signed 16-bit range (-32768..=32767). The Bedrock protocol encodes this field as an i16, so larger values would corrupt the packet. The write fails fast with InvalidInput instead of emitting a malformed packet.
Solutions
- Clamp durability_correction to -32768..=32767 before building the packet.
- Check the computation that produces the value for i32/i64 leakage into an i16 field.
- Return or log a domain error instead of silently wrapping when the value doesn't fit.
Example fix
// before let correction = VarInt(damage_delta as i32); // after let correction = VarInt(damage_delta.clamp(-32768, 32767) as i32);
Defensive patterns
Strategy: validation
Validate before calling
fn valid_correction(v: i32) -> bool { (-32768..=32767).contains(&v) }
// assert before building the packet:
debug_assert!(valid_correction(durability_correction.0)); Type guard
fn fits_i16(v: i64) -> Option<i16> { i16::try_from(v).ok() } Try / catch
packet.write(&mut writer).map_err(|e| {
log::error!("item stack response encode failed: {e}");
e
})?; Prevention
- Clamp durability deltas to i16 at the computation site.
- Prefer i16::try_from over as-casts so overflow is caught early.
- Add unit tests around min/max durability values.
When it happens
Trigger: Constructing an ItemStackResponseSlotInfo with durability_correction.0 outside -32768..=32767 and serializing the packet with PacketWrite::write.
Common situations: Computing durability correction from an i32/u32 item damage value without clamping; arithmetic overflow when aggregating durability deltas.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- item stack response count exceeds 4096
- item string too long
- user_data_len exceeds 1MB limit
- extra_data_len exceeds 1MB limit
- Invalid ContainerName ID
AI-assisted analysis of Pumpkin-MC/Pumpkin@8d4639e25a (2026-09-09).
Data as JSON: /api/errors/06a47549a3bb5f26.
Report an issue: GitHub.
Appendix: source
Thrown at crates/pumpkin-protocol/src/bedrock/client/item_stack_response.rs:24
serial::PacketWrite,
};
use pumpkin_macros::packet;
#[derive(Debug, Clone)]
pub struct ItemStackResponseSlotInfo {
pub requested_slot: u8,
pub slot: u8,
pub amount: u8,
pub item_stack_net_id: VarInt,
pub custom_name: String,
pub filtered_custom_name: String,
pub durability_correction: VarInt,
}
impl PacketWrite for ItemStackResponseSlotInfo {
fn write<W: Write>(&self, writer: &mut W) -> Result<(), Error> {
if !(-32768..=32767).contains(&self.durability_correction.0) {
return Err(Error::new(
std::io::ErrorKind::InvalidInput,
"durability correction must fit in an i16",
));
}
self.requested_slot.write(writer)?;
self.slot.write(writer)?;
self.amount.write(writer)?;
true.write(writer)?;
(self.item_stack_net_id.0 > 0).write(writer)?;
if self.item_stack_net_id.0 > 0 {
self.item_stack_net_id.write(writer)?;
}
self.custom_name.write(writer)?;
self.filtered_custom_name.write(writer)?;
self.durability_correction.write(writer)
}
}
View on GitHub (pinned to 8d4639e25a)