summary refs log tree commit diff stats
path: root/tests/ui/no_uaf_1.rs
blob: e4d7ac4638b00fe3c9977c11b6e206649340b111 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use core::cell::Cell;

use selfref::Holder;
use selfref::NewWith;
use selfref::opaque;

mod bad {
    use super::*;
    use std::rc::Rc;
    struct Foo<'a> {
        x: String,
        y: Rc<Cell<&'a str>>,
    }
    struct FooOpaque;
    opaque! {
        impl Opaque for FooOpaque {
            type Kind<'a> = Foo<'a>;
        }
    }
    pub fn test() {
        let rc = Rc::new(Cell::new(""));
        struct FooBuilder<'b>(Rc<Cell<&'b str>>);
        impl<'k, 'b> NewWith<'k, FooOpaque> for FooBuilder<'b> {
            fn new_with<'a>(self) -> Foo<'a> where 'k: 'a {
                Foo {
                    x: "Hello".to_owned(),
                    y: self.0,
                }
            }
        }
        let x = Holder::new_with(FooBuilder(rc.clone()));
        let x = Box::pin(x);
        x.as_ref().operate_in(|foo| {
            let foo = foo.get_ref();
            foo.y.set(&foo.x);
        });
        dbg!(&rc);
        drop(x);
        dbg!(&rc);
    }
}
fn main() {
    bad::test();
}