{"record":{"id":"55f0be99eb2e443e","repo":"embassy-rs/embassy","slug":"datalength-8","errorCode":null,"errorMessage":"DataLength > 8","messagePattern":"DataLength > 8","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"embassy-stm32/src/can/fd/message_ram/enums.rs","lineNumber":19,"sourceCode":"// Note: This file is copied and modified from fdcan crate by Richard Meadows\n\n/// Datalength is the message length generalised over\n/// the Standard (Classic) and FDCAN message types\n\n#[derive(Clone, Copy, Debug, PartialEq)]\npub enum DataLength {\n    Classic(u8),\n    Fdcan(u8),\n}\nimpl DataLength {\n    /// Creates a DataLength type\n    ///\n    /// Uses the byte length and Type of frame as input\n    pub fn new(len: u8, ff: FrameFormat) -> DataLength {\n        match ff {\n            FrameFormat::Classic => match len {\n                0..=8 => DataLength::Classic(len),\n                _ => panic!(\"DataLength > 8\"),\n            },\n            FrameFormat::Fdcan => match len {\n                0..=64 => DataLength::Fdcan(len),\n                _ => panic!(\"DataLength > 64\"),\n            },\n        }\n    }\n    /// Specialised function to create classic frames\n    pub fn new_classic(len: u8) -> DataLength {\n        Self::new(len, FrameFormat::Classic)\n    }\n    /// Specialised function to create FDCAN frames\n    pub fn new_fdcan(len: u8) -> DataLength {\n        Self::new(len, FrameFormat::Fdcan)\n    }\n\n    /// returns the length in bytes\n    pub fn len(&self) -> u8 {","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-stm32/src/can/fd/message_ram/enums.rs#L1-L37","documentation":"`DataLength::new` panics when a classic (non-FD) CAN frame is given a payload length greater than 8 bytes. Classic CAN frames are limited to 8 data bytes by the protocol, so the library refuses to represent a larger length instead of silently truncating.","triggerScenarios":"Calling `DataLength::new(len, FrameFormat::Classic)` with `len > 8`, or `DataLength::new_classic(len)` with len > 8; typically from building a classic TxFrame with an oversized buffer.","commonSituations":"Reusing a 64-byte FD payload with a classic frame configuration; user code that copies a large buffer into a classic CAN message; migration from FDCAN back to classic CAN without shrinking payloads.","solutions":["Limit classic frame payloads to 8 bytes or less before constructing DataLength.","Switch the frame format to FrameFormat::Fdcan if >8-byte payloads are actually needed.","Use `len.min(8)` only if truncation is acceptable for your application."],"exampleFix":"// before\nlet dl = DataLength::new(payload.len() as u8, FrameFormat::Classic);\n// after\nlet dl = DataLength::new(\n    payload.len() as u8,\n    if payload.len() > 8 { FrameFormat::Fdcan } else { FrameFormat::Classic },\n);","handlingStrategy":"validation","validationCode":"fn valid_classic_len(len: u8) -> bool { len <= 8 }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always check payload length against the frame format limit (8 classic / 64 FD).","Slice large payloads into multiple frames before sending.","Centralize frame construction in a helper that enforces the limits."],"tags":["embedded","can","panic","payload-size"],"backgroundTag":"value-out-of-range","analyzedSha":"463a07b963419a1bfe61d5d597c44acb810afb8b","analyzedAt":"2026-09-10T13:38:26.660Z","contentChangedAt":"2026-09-10T13:38:26.660Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}