Skip to content

What about: validity after/during drop #268

Closed
@CAD97

Description

@CAD97

e.g. Box<T> potentially has a validity requirement of being dereferencable. <Box<T> as Drop>::drop obviously breaks this requirement, and at the end of the function, the Box<T> still exists, but the pointer is now dangling.

That one is probably directly handled by #[may_dangle], so what about this minimal example:

struct Weird(NonZeroU8);

impl Drop for Weird {
    fn drop(&mut self) {
        unsafe { ptr::write(self as *mut Weird as *mut u8, 0u8) }; // <--- HERE
    }
}

fn main() {
    let w = Some(Weird(NonZeroU8::new(1).unwrap()));
    drop(w);
}

Does the indicated line invoke UB? When the allocated place is Weird? When the allocated place is Option<Weird> (#204 IIRC)?

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