Skip to content

causes UB by calling mem::zeroed on any type #2

Closed
@RalfJung

Description

@RalfJung

This code is highly problematic:

    pub unsafe fn new<F: for<'a> FnOnce(&'a T) -> &'a U>(f: F) -> Self {
        // Construct a "fake" T. It's not valid, but the lambda shouldn't
        // actually access it (which is why this is unsafe)
        let x = mem::zeroed();

If T is, for example, a struct containing reference type, then this causes UB by initializing a reference with 0. The comment seems to say that it is okay to do this as long as x is not accessed; that is not correct.

That said, there currently is no correct way to do what you want. This bug can only really be fixed once MaybeUninit gets stabilized.

(I also feel really uneasy with the extend to which this relies on type inference, but YMMV.^^)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions