-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from steffahn/prevent_unsound_field_swapping
Prevent unsound field swapping
- Loading branch information
Showing
5 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: Rust | ||
|
||
on: [push] | ||
on: [push, pull_request] | ||
|
||
jobs: | ||
native: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use self_cell::self_cell; | ||
|
||
type Dep1<'a> = (&'a str, &'static str); | ||
|
||
self_cell! { | ||
pub struct Struct1 { | ||
owner: String, | ||
#[covariant] | ||
dependent: Dep1, | ||
} | ||
} | ||
|
||
type Dep2<'a> = (&'static str, &'a str); | ||
|
||
self_cell! { | ||
pub struct Struct2 { | ||
owner: String, | ||
#[covariant] | ||
dependent: Dep2, | ||
} | ||
} | ||
|
||
fn main() { | ||
let hello: &'static str; | ||
{ | ||
let mut x1 = Struct1::new(String::from("Hello World"), |s| (s, "")); | ||
let mut x2 = Struct2::new(String::new(), |_| ("", "")); | ||
std::mem::swap(&mut x1.unsafe_self_cell, &mut x2.unsafe_self_cell); | ||
hello = x2.borrow_dependent().0; | ||
|
||
dbg!(hello); // "Hello World" | ||
// hello is now a static reference in to the "Hello World" string | ||
} | ||
// the String is dropped at the end of the block above | ||
|
||
dbg!(hello); // prints garbage, use-after-free | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/swap_cell_member.rs:28:50 | ||
| | ||
28 | std::mem::swap(&mut x1.unsafe_self_cell, &mut x2.unsafe_self_cell); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `Struct1`, found struct `Struct2` | ||
| | ||
= note: expected mutable reference `&mut UnsafeSelfCell<Struct1, String, (&'static str, &'static str)>` | ||
found mutable reference `&mut UnsafeSelfCell<Struct2, String, (&'static str, &'static str)>` |