{"record":{"id":"407bdee41651f877","repo":"n0-computer/iroh","slug":"slice-with-incorrect-length","errorCode":null,"errorMessage":"slice with incorrect length","messagePattern":"slice with incorrect length","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"iroh-base/src/key.rs","lineNumber":146,"sourceCode":"\n    /// Verify a signature on a message with this public key.\n    ///\n    /// # Return\n    ///\n    /// Returns `Ok(())` if the signature is valid, and `Err` otherwise.\n    pub fn verify(&self, message: &[u8], signature: &Signature) -> Result<(), SignatureError> {\n        self.as_verifying_key()\n            .verify_strict(message, &signature.0)\n            .map_err(|_| SignatureError::new())\n    }\n\n    /// Convert to a hex string limited to the first 5 bytes for a friendly string\n    /// representation of the key.\n    pub fn fmt_short(&self) -> impl Display + Copy + 'static {\n        PublicKeyShort(\n            self.0.as_bytes()[0..5]\n                .try_into()\n                .expect(\"slice with incorrect length\"),\n        )\n    }\n\n    /// Needed for internal conversions, not part of the stable API.\n    #[doc(hidden)]\n    pub fn as_verifying_key(&self) -> VerifyingKey {\n        VerifyingKey::from_bytes(self.0.as_bytes()).expect(\"already verified\")\n    }\n\n    /// Needed for internal conversions, not part of the stable API.\n    #[doc(hidden)]\n    pub fn from_verifying_key(key: VerifyingKey) -> Self {\n        Self(CompressedEdwardsY(key.to_bytes()))\n    }\n\n    /// Encodes this key in [z-base-32](https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt),\n    /// the encoding used by [pkarr](https://pkarr.org) domain names.\n    pub fn to_z32(&self) -> String {","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/n0-computer/iroh/blob/2b4de030ce5e0133f272871a76f0c685c63f552a/iroh-base/src/key.rs#L128-L164","documentation":"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.","triggerScenarios":"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.","commonSituations":"Seen when reading/refactoring key.rs; a runtime panic here would mean PublicKey no longer holds 32 bytes or the literal range was edited.","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."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Not applicable for users: fmt_short cannot panic; nothing to pre-validate.\nnull","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["internal-invariant","display","panic-guard"],"backgroundTag":"internal-invariant-violation","analyzedSha":"2b4de030ce5e0133f272871a76f0c685c63f552a","analyzedAt":"2026-09-08T04:26:47.755Z","contentChangedAt":"2026-09-08T04:26:47.755Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}