Closed
Description
I tried this code:
use std::sync::Mutex;
static MUTEX: Mutex<Option<&'static str>> = Mutex::new(None);
trait Foo {
fn foo<'a: 'static>(&self) -> impl Sized;
}
impl Foo for str {
fn foo<'a: 'static>(&'a self) -> impl Sized + 'a {
*MUTEX.lock().unwrap() = Some(self);
}
}
fn call_foo<T: Foo + ?Sized>(s: &T) {
s.foo();
}
fn main() {
let s = String::from("hello, world");
call_foo(s.as_str());
drop(s);
println!("> {}", MUTEX.lock().unwrap().unwrap());
}
I expected to see this happen: Compilation failure.
Instead, this happened: Segfault due to UAF
Why?
See comment below.
Meta
rustc --version --verbose
:
2024-11-24
Not present on beta or stable.
Metadata
Metadata
Assignees
Labels
Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.Category: This is a bug.`#![feature(return_position_impl_trait_in_trait)]`Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessHigh priorityRelevant to the types team, which will review and decide on the PR/issue.
Type
Projects
Status
Completed