rust/tests/fail/environ-gets-deallocated.rs

25 lines
684 B
Rust
Raw Normal View History

//@ignore-target-windows: Windows does not have a global environ list that the program can access directly
2020-03-07 10:35:00 -06:00
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
2020-03-07 10:35:00 -06:00
fn get_environ() -> *const *const u8 {
extern "C" {
static mut environ: *const *const u8;
}
unsafe { environ }
2020-03-07 10:35:00 -06:00
}
#[cfg(target_os = "macos")]
2020-03-07 10:35:00 -06:00
fn get_environ() -> *const *const u8 {
extern "C" {
fn _NSGetEnviron() -> *mut *const *const u8;
}
unsafe { *_NSGetEnviron() }
}
fn main() {
2020-03-07 10:35:00 -06:00
let pointer = get_environ();
let _x = unsafe { *pointer };
std::env::set_var("FOO", "BAR");
let _y = unsafe { *pointer }; //~ ERROR: dereferenced after this allocation got freed
}