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

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

Open
ia0 opened this issue Jul 18, 2023 · 0 comments
Open

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

ia0 opened this issue Jul 18, 2023 · 0 comments

Comments

@ia0
Copy link
Contributor

ia0 commented Jul 18, 2023

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.

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

No branches or pull requests

1 participant