Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic on Changing set_dst_pan_id Value in Packet Creation Example #29

Closed
AmirAliEbrahimi opened this issue Dec 16, 2024 · 1 comment · Fixed by #30
Closed

Panic on Changing set_dst_pan_id Value in Packet Creation Example #29

AmirAliEbrahimi opened this issue Dec 16, 2024 · 1 comment · Fixed by #30

Comments

@AmirAliEbrahimi
Copy link

Hello,

I encountered a panic when modifying the set_dst_pan_id value in the following packet creation example:

use dot15d4_frame::*;

fn main() {
    let frame = FrameBuilder::new_data(&[0x2b, 0x00, 0x00, 0x00])
        .set_sequence_number(1)
        .set_dst_pan_id(0xabcd)
        .set_dst_address(Address::Short([0x02, 0x04]))
        .set_src_pan_id(0xabcd)
        .set_src_address(Address::Extended([
            0x00, 0x12, 0x4b, 0x00, 0x14, 0xb5, 0xd9, 0xc7,
        ]))
        .finalize()
        .unwrap();

    let mut buffer = vec![0; frame.buffer_len()];

    frame.emit(&mut Frame::new_unchecked(&mut buffer[..]));

    println!("{:?}", frame);
    println!("packet = {:#04X?}", buffer);
}

This works fine and prints the packet as expected. However, if I change set_dst_pan_id to 0xabce, the program panics with the following error:

thread 'main' panicked at /home/aebrahimi/Projects/dot15d4/dot15d4-frame/src/lib.rs:469:40:
source slice length (4) does not match destination slice length (2)
stack backtrace:
   0: rust_begin_unwind
             at /builddir/build/BUILD/rust-1.83.0-build/rustc-1.83.0-src/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /builddir/build/BUILD/rust-1.83.0-build/rustc-1.83.0-src/library/core/src/panicking.rs:74:14
   2: core::slice::<impl [T]>::copy_from_slice::len_mismatch_fail
             at /builddir/build/BUILD/rust-1.83.0-build/rustc-1.83.0-src/library/core/src/slice/mod.rs:3685:13
   3: core::slice::<impl [T]>::copy_from_slice
             at /builddir/build/BUILD/rust-1.83.0-build/rustc-1.83.0-src/library/core/src/slice/mod.rs:3692:13
   4: dot15d4_frame::Frame<T>::set_payload
             at /home/aebrahimi/Projects/dot15d4/dot15d4-frame/src/lib.rs:469:9
   5: dot15d4_frame::repr::FrameRepr::emit
             at /home/aebrahimi/Projects/dot15d4/dot15d4-frame/src/repr/mod.rs:93:13
   6: test_app::main
             at ./src/main.rs:16:5
   7: core::ops::function::FnOnce::call_once
             at /builddir/build/BUILD/rust-1.83.0-build/rustc-1.83.0-src/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Analysis:

Based on the error, it seems that the buffer length mismatch occurs because frame.buffer_len() reports the wrong size. This results in an incorrect buffer length during the emission, leading to the panic.

Could you please confirm if this is the expected behavior, or if there's something else causing the issue?

Thank you!

@thvdveld
Copy link
Owner

Thank you for filing an issue! The problem was that the builder omitted the source PAN ID. It is fixed in #30 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants