- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Closed
Labels
C-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: 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.Relevant to the library API team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.This issue requires a nightly compiler in some way.
Description
#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
matklad, nagisa and KodrAus
Metadata
Metadata
Assignees
Labels
C-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: 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.Relevant to the library API team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.This issue requires a nightly compiler in some way.