Clippy says: "call for this type may be undefined behavior" - is it?

Don't assume it's initialized before it's actually initialized.

    let cttyname = std::ffi::CString::new("/dev/tty").unwrap(); 
    let mut state = MaybeUninit::<libc::termios>::uninit();
    let state = unsafe {
        let ctty = libc::open(cttyname.as_ptr(), libc::O_RDONLY | libc::O_CLOEXEC);
        // Assumption: this call writes all fields of `state`
        // without/before reading any fields of `state`
        libc::tcgetattr(ctty, state.as_mut_ptr()); 
        state.assume_init()
    };

See here.

5 Likes