summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2022-12-04 17:15:19 -0300
committerSoniEx2 <endermoneymod@gmail.com>2022-12-04 17:15:19 -0300
commit2b5d0cb87b13fd5354143bb0b802d06ca0715ca6 (patch)
treefbe604b8f5476a2d1a6605395a0b576b6062b409 /tests
parent72519f2161672a2edcd393a41f9f2cd01eddc22f (diff)
Add test by steffahn
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/no_uaf_2.rs33
-rw-r--r--tests/ui/no_uaf_2.stderr10
2 files changed, 43 insertions, 0 deletions
diff --git a/tests/ui/no_uaf_2.rs b/tests/ui/no_uaf_2.rs
new file mode 100644
index 0000000..eb6b7d3
--- /dev/null
+++ b/tests/ui/no_uaf_2.rs
@@ -0,0 +1,33 @@
+// by steffahn
+
+#![feature(pin_macro)]
+
+use std::cell::Cell;
+use std::pin::pin;
+
+use selfref::opaque;
+use selfref::{new_with_closure, Holder};
+
+struct MyStruct<'this> {
+    cell: Cell<&'this str>,
+}
+
+struct MyStructKey;
+
+opaque! {
+    impl Opaque for MyStructKey {
+        type Kind<'this> = MyStruct<'this>;
+    }
+}
+
+fn main() {
+    let s =
+        Holder::<'_, MyStructKey>::new_with(new_with_closure::<MyStructKey, _>(|[]| MyStruct {
+            cell: Default::default(),
+        }));
+    let s = pin!(s);
+    s.as_ref().operate_in(|r| {
+        r.cell.set(&String::from("hello world")); // temporary dropped at end of this statement
+        println!("{}", r.cell.get()) // accesses dropped `String` data
+    });
+}
diff --git a/tests/ui/no_uaf_2.stderr b/tests/ui/no_uaf_2.stderr
new file mode 100644
index 0000000..5737a38
--- /dev/null
+++ b/tests/ui/no_uaf_2.stderr
@@ -0,0 +1,10 @@
+error[E0716]: temporary value dropped while borrowed
+  --> tests/ui/no_uaf_2.rs:30:21
+   |
+29 |     s.as_ref().operate_in(|r| {
+   |                            - has type `OperateIn<'1, MyStructKey>`
+30 |         r.cell.set(&String::from("hello world")); // temporary dropped at end of this statement
+   |         ------------^^^^^^^^^^^^^^^^^^^^^^^^^^^-- temporary value is freed at the end of this statement
+   |         |           |
+   |         |           creates a temporary value which is freed while still in use
+   |         argument requires that borrow lasts for `'1`