n0-computer/iroh · info

data length checked above

Error message

data length checked above

What it means

CustomAddr::from_bytes first checks `data.len() < 8` and returns "data too short"; after that check it slices the first 8 bytes and converts them with try_into().expect("data length checked above"). The panic is an internal invariant: it can only fire if the length check above was bypassed, so it documents that the slice is guaranteed to be exactly 8 bytes at that point.

Solutions

  1. No action needed at runtime — the guard above always runs first; treat the expect as documentation of the invariant.
  2. If refactoring from_bytes, keep the `data.len() < 8` early-return immediately before the `data[..8].try_into()` call.
  3. Prefer a non-panicking rewrite (match on try_into) if you remove the early return.
Defensive patterns

Strategy: validation

Validate before calling

// Caller-side: from_bytes already returns Result, so handle the 'data too short' case
fn parse_custom_addr(bytes: &[u8]) -> Result<CustomAddr, String> {
    if bytes.len() < 8 { return Err(format!("need >= 8 bytes, got {}", bytes.len())); }
    CustomAddr::from_bytes(bytes).map_err(|e| e.to_string())
}

Try / catch

// The function returns Result, not panics — match on it
match CustomAddr::from_bytes(&buf) {
    Ok(addr) => use_addr(addr),
    Err("data too short") => warn("truncated CustomAddr payload"),
    Err(e) => warn("CustomAddr decode failed: {e}"),
}

Prevention

When it happens

Trigger: Not reachable through normal execution: the preceding `if data.len() < 8 { return Err("data too short") }` guarantees the 8-byte slice succeeds. Only reachable if the code is modified so the guard no longer precedes the try_into.

Common situations: Developers encounter this string only while reading or refactoring the source; a panic here at runtime would indicate a code regression where the length guard was moved or removed.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of n0-computer/iroh@2b4de030ce (2026-09-08). Data as JSON: /api/errors/3aef3f1c67b36cc8. Report an issue: GitHub.

Appendix: source

Thrown at iroh-base/src/endpoint_addr.rs:380

    /// Serializes to the binary encoding.
    ///
    /// See [`CustomAddr`] docs for details on the encoding.
    pub fn to_vec(&self) -> Vec<u8> {
        let mut out = vec![0u8; 8 + self.data.len()];
        out[..8].copy_from_slice(&self.id().to_le_bytes());
        out[8..].copy_from_slice(self.data());
        out
    }

    /// Parses from the binary encoding.
    ///
    /// See [`CustomAddr`] docs for details on the encoding.
    pub fn from_bytes(data: &[u8]) -> Result<Self, &'static str> {
        if data.len() < 8 {
            return Err("data too short");
        }
        let id = u64::from_le_bytes(data[..8].try_into().expect("data length checked above"));
        let data = &data[8..];
        Ok(Self::from_parts(id, data))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
    #[non_exhaustive]
    enum NewAddrType {
        /// Relays
        Relay(RelayUrl),
        /// IP based addresses
        Ip(SocketAddr),
        /// New addr type for testing
        Cool(u16),

View on GitHub (pinned to 2b4de030ce)