Skip to content

#[repr(C)] and Drop causes undefined behavior in FFI code #18380

Closed
@mahkoh

Description

@mahkoh
extern {
    fn f(x: *const X);
}

#[repr(C)]
struct X {
    i: i32,
}

impl Drop for X {
    fn drop(&mut self) { }
}

fn main() {
    let x = [X { i: 0 }, X { i: 0 }];
    println!("{}", std::mem::size_of_val(&x));
    unsafe { f(x.as_ptr()); }
}

Prints 16.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.P-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions