Rust

Rust

Made by DeepSource

Found use of std::ffi::c_void ptr in from_raw RS-W1118

Anti-pattern
Minor

Passing a c_void raw pointer to {Box,Rc,Arc,Weak}::from_raw(_) results in a Box<c_void> or Rc<c_void> or Weak<c_void>, which would need to have the same memory layout as the expected type T. This is often not the case and can lead to undefined behaviour, creating hard-to-track bugs.

Bad practice

fn foo() {
    let ptr = Box::into_raw(Box::new(42usize)) as *mut c_void;
    let _ = unsafe { Box::from_raw(ptr) };
}

Recommended

fn foo() {
    let _ = unsafe { Box::from_raw(ptr as *mut usize) };
}