Skip to content

Soundness issue in Drop for SyncOnceCell #76367

Closed
@m-ou-se

Description

@m-ou-se

#75648 added #[may_dangle] to T in the Drop implementation of SyncOnceCell. This is correct for simple types like T = &str, but when T's Drop implementation accesses borrowed data, this might lead to accessing already dropped data:

#![feature(once_cell)]

use std::lazy::SyncOnceCell;

struct A<'a>(&'a str);

impl<'a> Drop for A<'a> {
    fn drop(&mut self) {
        dbg!(self.0);
    }
}

fn main() {
        let cell = SyncOnceCell::new();
        {
            let s = String::from("hello world");
            let _ = cell.set(A(&s));
        }
}
[src/main.rs:9] self.0 = "\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{10}thread 'main' panicked at 'byte index 9 is not a char boundary; it is inside '\u{10}' (bytes 8..9) of `À`', library/core/src/fmt/mod.rs:2043:22
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions