2022-07-17 20:54:10 -05:00
|
|
|
//@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
|
|
|
|
2022-07-01 14:28:45 -05:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
|
2020-03-07 10:35:00 -06:00
|
|
|
fn get_environ() -> *const *const u8 {
|
2022-06-19 22:33:59 -05:00
|
|
|
extern "C" {
|
|
|
|
static mut environ: *const *const u8;
|
|
|
|
}
|
|
|
|
unsafe { environ }
|
2020-03-07 10:35:00 -06:00
|
|
|
}
|
|
|
|
|
2022-06-19 22:33:59 -05: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() }
|
2020-03-07 08:26:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-03-07 10:35:00 -06:00
|
|
|
let pointer = get_environ();
|
2020-03-07 08:26:04 -06:00
|
|
|
let _x = unsafe { *pointer };
|
|
|
|
std::env::set_var("FOO", "BAR");
|
2022-07-11 06:44:55 -05:00
|
|
|
let _y = unsafe { *pointer }; //~ ERROR: dereferenced after this allocation got freed
|
2020-03-07 08:26:04 -06:00
|
|
|
}
|