n0-computer/iroh · info
slice with incorrect length
Error message
slice with incorrect length
What it means
PublicKey::fmt_short slices the key's 32 bytes to the first 5 and converts to a fixed-size array for a short hex display, expecting "slice with incorrect length" never to fire. The panic is impossible at runtime because the slice bounds [0..5] are hardcoded to 5 bytes, matching the target array length; it exists only to satisfy the compiler's try_into Result.
Solutions
- No runtime action required; the expect documents a guaranteed-length slice.
- If changing key representation, update both the slice range and the array size together, or handle try_into's error explicitly.
- Keep unit tests asserting fmt_short output length to catch such regressions.
Defensive patterns
Strategy: validation
Validate before calling
// Not applicable for users: fmt_short cannot panic; nothing to pre-validate. null
Prevention
- No user-side guard needed — the 5-byte slice is compile-time-fixed and always valid.
- When editing key.rs, keep the [0..5] range and the 5-byte array type in sync.
- Add a display-format test for fmt_short to catch accidental changes.
When it happens
Trigger: Not reachable: `self.0.as_bytes()[0..5]` always yields exactly 5 bytes (keys are 32 bytes), so try_into into the 5-byte array always succeeds. Reachable only if key size or the slice range is changed in a refactor.
Common situations: Seen when reading/refactoring key.rs; a runtime panic here would mean PublicKey no longer holds 32 bytes or the literal range was edited.
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/407bdee41651f877.
Report an issue: GitHub.
Appendix: source
Thrown at iroh-base/src/key.rs:146
/// Verify a signature on a message with this public key.
///
/// # Return
///
/// Returns `Ok(())` if the signature is valid, and `Err` otherwise.
pub fn verify(&self, message: &[u8], signature: &Signature) -> Result<(), SignatureError> {
self.as_verifying_key()
.verify_strict(message, &signature.0)
.map_err(|_| SignatureError::new())
}
/// Convert to a hex string limited to the first 5 bytes for a friendly string
/// representation of the key.
pub fn fmt_short(&self) -> impl Display + Copy + 'static {
PublicKeyShort(
self.0.as_bytes()[0..5]
.try_into()
.expect("slice with incorrect length"),
)
}
/// Needed for internal conversions, not part of the stable API.
#[doc(hidden)]
pub fn as_verifying_key(&self) -> VerifyingKey {
VerifyingKey::from_bytes(self.0.as_bytes()).expect("already verified")
}
/// Needed for internal conversions, not part of the stable API.
#[doc(hidden)]
pub fn from_verifying_key(key: VerifyingKey) -> Self {
Self(CompressedEdwardsY(key.to_bytes()))
}
/// Encodes this key in [z-base-32](https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt),
/// the encoding used by [pkarr](https://pkarr.org) domain names.
pub fn to_z32(&self) -> String {View on GitHub (pinned to 2b4de030ce)