Created
February 28, 2024 01:26
-
-
Save DefectingCat/6668c200c757e638648ba2bc4c9c0dca to your computer and use it in GitHub Desktop.
rust memory vulnerability
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
fn main() { | |
let mut safety = &String::from("hello"); | |
println!("address of safety {:p}: {}", &safety, safety); | |
{ | |
let name = "xfy".to_string(); | |
let name = expand(&name); | |
println!("address of name {:p}: {}", &name, name); | |
safety = &name; | |
} | |
println!("address of safety {:p}: {}", &safety, safety); | |
} | |
pub const fn lifetime_translator<'a, 'b, T>(_val_a: &'a &'b (), _val_b: &'b T) -> &'a T { | |
_val_b | |
} | |
pub fn lifetime_translator_mut<'a, 'b, T>(_val_a: &'a &'b (), _val_b: &'b mut T) -> &'a mut T { | |
_val_b | |
} | |
pub fn expand<'a, 'b, T>(x: &'a T) -> &'b T { | |
// lifetime_translator(STATIC_UNIT, x) | |
let f: fn(_, &'a T) -> &'b T = lifetime_translator; | |
f(STATIC_UNIT, x) | |
} | |
pub fn expand_mut<'a, 'b, T>(x: &'a mut T) -> &'b mut T { | |
let f: fn(_, &'a mut T) -> &'b mut T = lifetime_translator_mut; | |
f(STATIC_UNIT, x) | |
} | |
const STATIC_UNIT: &&() = &&(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment