Skip to content

Possible undefined behavior in <Usbd as UsbBus>::write() #15

@ia0

Description

@ia0

nrf-usbd/src/usbd.rs

Lines 378 to 383 in cbd7ed7

let mut ram_buf: MaybeUninit<[u8; 64]> = MaybeUninit::uninit();
unsafe {
let slice = &mut *ram_buf.as_mut_ptr();
slice[..buf.len()].copy_from_slice(buf);
}
let ram_buf = unsafe { ram_buf.assume_init() };

Line 380 looks like UB because we're creating a mutable reference to unitialized memory. Some write method (from MaybeUninit or the raw pointer type) should be used.

Line 383 also looks like UB because we did not initialize the whole array.

Probably the simpler solution is to use a regular array and avoid usage of unsafe.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions