{"record":{"id":"bf5b09cb10abad7c","repo":"atuinsh/atuin","slug":"writing-to-a-vec-is-infallible","errorCode":null,"errorMessage":"writing to a Vec is infallible","messagePattern":"writing to a Vec is infallible","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"crates/atuin-common/src/encryption/paseto_v4.rs","lineNumber":173,"sourceCode":"\n        p_self.wrap_pie(&p_wrapping)\n    }\n\n    /// Generate a new key with the XSalsa20Poly1305 algorithm.\n    #[must_use]\n    pub fn generate() -> Self {\n        <[u8; 32]>::from(XSalsa20Poly1305::generate_key(&mut aead::OsRng)).into()\n    }\n\n    /// Encode this key into a B64-encoded string, if possible.\n    #[must_use]\n    pub fn encode(&self) -> PlainTextEncodedKey {\n        let key_bytes = self.as_bytes();\n        // A msgpack array16 header (3 bytes) followed by each byte as at most a 2-byte uint.\n        let mut buf = Vec::with_capacity(3 + 2 * key_bytes.len());\n        // Writing to a `Vec` is infallible, so neither of these can actually error.\n        rmp::encode::write_array_len(&mut buf, u32::conv(key_bytes.len()))\n            .expect(\"writing to a Vec is infallible\");\n        for b in key_bytes {\n            rmp::encode::write_uint(&mut buf, u64::from(*b))\n                .expect(\"writing to a Vec is infallible\");\n        }\n\n        PlainTextEncodedKey(KEY_ENCODER.encode(buf))\n    }\n\n    pub fn decode(key: &str) -> Result<Self, KeyDecodingError> {\n        let buf = KEY_ENCODER.decode(key.trim_end())?;\n\n        // Legacy code used to naively encode the base64 string into the string. New code does this\n        // rmp dance.\n        match <[u8; 32]>::try_from(&*buf) {\n            Ok(key) => Ok(key.into()),\n            Err(_) => {\n                if buf.is_empty() {\n                    return Err(KeyDecodingError::EmptyKey);","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/atuinsh/atuin/blob/900007c48045867e7f44b0f9bc55ad9dbe99ee0d/crates/atuin-common/src/encryption/paseto_v4.rs#L155-L191","documentation":"`encode` builds a MessagePack representation of a PASETO v4 key inside a `Vec<u8>` and asserts each `rmp::encode::write_*` result with `.expect(\"writing to a Vec is infallible\")`. rmp encode functions only fail on unsupported types/values; writing a small array header and single-byte uints into a `Vec` is provably infallible, so this is an invariant assertion.","triggerScenarios":"Only reachable if the rmp encode functions start failing for these trivially valid arguments — effectively never; guarded by a breaking change of the rmp crate.","commonSituations":"Runs on every key save path (`try_write_path`, `overwrite_path`) when persisting the plaintext-encoded key.","solutions":["No action needed; if rmp API changes, convert to returning a Result and propagate"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// infallible for Vec targets; if rmp API changes:\nlet _ = rmp::encode::write_array_len(&mut buf, u32::conv(key_bytes.len()));","preventionTips":["Pin the rmp crate version","Keep the pre-reserved capacity (3 + 2 * len) to guarantee no reallocation-related concerns","Encode only values ≤ u8::MAX in the loop"],"tags":["panic","infallible","msgpack","encryption"],"backgroundTag":"internal-invariant-violation","analyzedSha":"900007c48045867e7f44b0f9bc55ad9dbe99ee0d","analyzedAt":"2026-09-19T12:52:51.491Z","contentChangedAt":"2026-09-19T12:52:51.491Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}