2020-06-28 11:31:34 +02:00
|
|
|
// ignore-windows: Windows does not have a global environ list that the program can access directly
|
2020-03-07 11:35:00 -05:00
|
|
|
|
2022-06-19 20:33:59 -07:00
|
|
|
#[cfg(target_os = "linux")]
|
2020-03-07 11:35:00 -05:00
|
|
|
fn get_environ() -> *const *const u8 {
|
2022-06-19 20:33:59 -07:00
|
|
|
extern "C" {
|
|
|
|
static mut environ: *const *const u8;
|
|
|
|
}
|
|
|
|
unsafe { environ }
|
2020-03-07 11:35:00 -05:00
|
|
|
}
|
|
|
|
|
2022-06-19 20:33:59 -07:00
|
|
|
#[cfg(target_os = "macos")]
|
2020-03-07 11:35:00 -05:00
|
|
|
fn get_environ() -> *const *const u8 {
|
|
|
|
extern "C" {
|
|
|
|
fn _NSGetEnviron() -> *mut *const *const u8;
|
|
|
|
}
|
|
|
|
unsafe { *_NSGetEnviron() }
|
2020-03-07 09:26:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-03-07 11:35:00 -05:00
|
|
|
let pointer = get_environ();
|
2020-03-07 09:26:04 -05:00
|
|
|
let _x = unsafe { *pointer };
|
|
|
|
std::env::set_var("FOO", "BAR");
|
2020-03-08 23:34:54 +01:00
|
|
|
let _y = unsafe { *pointer }; //~ ERROR dereferenced after this allocation got freed
|
2020-03-07 09:26:04 -05:00
|
|
|
}
|