-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unsound cyclic re-borrow #28
Comments
Or shall we say, I’m comparing to ouroboros right now. And it’s not directly applicable, yet with some additional effort, there’s a bug to be found in ouroboros, too 🎉 use std::cell::RefCell;
use ouroboros::self_referencing;
struct Bar<'a>(RefCell<(Option<&'a Bar<'a>>, String)>);
#[self_referencing]
struct Foo {
owner: (),
#[borrows(owner)]
#[not_covariant]
bar: Bar<'this>,
#[borrows(bar)]
#[not_covariant]
baz: &'this Bar<'this>,
}
impl Drop for Bar<'_> {
fn drop(&mut self) {
let r1 = self.0.get_mut();
let string_ref_1 = &mut r1.1;
let mut r2 = r1.0.unwrap().0.borrow_mut();
let string_ref_2 = &mut r2.1;
let s = &string_ref_1[..];
string_ref_2.clear();
string_ref_2.shrink_to_fit();
println!("{}", s); // prints garbage
}
}
fn main() {
Foo::new(
(),
|_| Bar(RefCell::new((None, "Hello World!".to_owned()))),
|bar| {
bar.0.borrow_mut().0 = Some(bar);
bar
},
);
} |
To remove the issue in I’m not sure how much breakage comes with such a change, though 😅 |
Could you please elaborate what you mean. |
Ah, sorry, I wanted to elaborate anyways, but I kind-of forgot about this issue over reporting the ouroboros one and even finding another ouroboros issue. Comparing pub struct Bar<'a>(*mut &'a ());
self_cell! {
pub struct Foo1 {
owner: (),
#[not_covariant]
dependent: Bar,
}
}
#[self_referencing]
pub struct Foo2 {
owner: (),
#[not_covariant]
#[borrows(owner)]
dependent: Bar<'this>,
}
Hence, suggested change to
😉 (feel free to use a different name for the lifetime if you want) Note that the |
The fix for the issue described in #28 (reborrow_dependent_cyclic.rs) allows compiling leak_dependent.rs which should not be possible. This is a first attempt and incomplete as seen by the stubbed out impls for with_dependent and with_dependent_mut. DO NOT MERGE
Small addition to my previous comment
Actually, the second condition regarding |
I’ll try to explain (and compare to ouroboros) tomorrow
The text was updated successfully, but these errors were encountered: