embassy-rs/embassy · error

DataLength > 8

Error message

DataLength > 8

What it means

`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.

Solutions

  1. Limit classic frame payloads to 8 bytes or less before constructing DataLength.
  2. Switch the frame format to FrameFormat::Fdcan if >8-byte payloads are actually needed.
  3. Use `len.min(8)` only if truncation is acceptable for your application.

Example fix

// before
let dl = DataLength::new(payload.len() as u8, FrameFormat::Classic);
// after
let dl = DataLength::new(
    payload.len() as u8,
    if payload.len() > 8 { FrameFormat::Fdcan } else { FrameFormat::Classic },
);
Defensive patterns

Strategy: validation

Validate before calling

fn valid_classic_len(len: u8) -> bool { len <= 8 }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of embassy-rs/embassy@463a07b963 (2026-09-10). Data as JSON: /api/errors/55f0be99eb2e443e. Report an issue: GitHub.

Appendix: source

Thrown at embassy-stm32/src/can/fd/message_ram/enums.rs:19

// Note: This file is copied and modified from fdcan crate by Richard Meadows

/// Datalength is the message length generalised over
/// the Standard (Classic) and FDCAN message types

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DataLength {
    Classic(u8),
    Fdcan(u8),
}
impl DataLength {
    /// Creates a DataLength type
    ///
    /// Uses the byte length and Type of frame as input
    pub fn new(len: u8, ff: FrameFormat) -> DataLength {
        match ff {
            FrameFormat::Classic => match len {
                0..=8 => DataLength::Classic(len),
                _ => panic!("DataLength > 8"),
            },
            FrameFormat::Fdcan => match len {
                0..=64 => DataLength::Fdcan(len),
                _ => panic!("DataLength > 64"),
            },
        }
    }
    /// Specialised function to create classic frames
    pub fn new_classic(len: u8) -> DataLength {
        Self::new(len, FrameFormat::Classic)
    }
    /// Specialised function to create FDCAN frames
    pub fn new_fdcan(len: u8) -> DataLength {
        Self::new(len, FrameFormat::Fdcan)
    }

    /// returns the length in bytes
    pub fn len(&self) -> u8 {

View on GitHub (pinned to 463a07b963)