Rollup merge of #128433 - hermit-os:hermit-unsafe_op_in_unsafe_fn, r=… · model-checking/verify-rust-std@ab00ae6

@@ -68,21 +68,21 @@ pub fn current_exe() -> io::Result<PathBuf> {

6868

unsupported()

6969

}

707071-

static mut ENV: Option<Mutex<HashMap<OsString, OsString>>> = None;

71+

static ENV: Mutex<Option<HashMap<OsString, OsString>>> = Mutex::new(None);

72727373

pub fn init_environment(env: *const *const i8) {

74-

unsafe {

75-

ENV = Some(Mutex::new(HashMap::new()));

74+

let mut guard = ENV.lock().unwrap();

75+

let map = guard.insert(HashMap::new());

767677-

if env.is_null() {

78-

return;

79-

}

77+

if env.is_null() {

78+

return;

79+

}

808081-

let mut guard = ENV.as_ref().unwrap().lock().unwrap();

81+

unsafe {

8282

let mut environ = env;

8383

while !(*environ).is_null() {

8484

if let Some((key, value)) = parse(CStr::from_ptr(*environ).to_bytes()) {

85-

guard.insert(key, value);

85+

map.insert(key, value);

8686

}

8787

environ = environ.add(1);

8888

}

@@ -154,30 +154,26 @@ impl Iterator for Env {

154154

/// Returns a vector of (variable, value) byte-vector pairs for all the

155155

/// environment variables of the current process.

156156

pub fn env() -> Env {

157-

unsafe {

158-

let guard = ENV.as_ref().unwrap().lock().unwrap();

159-

let mut result = Vec::new();

157+

let guard = ENV.lock().unwrap();

158+

let env = guard.as_ref().unwrap();

160159161-

for (key, value) in guard.iter() {

162-

result.push((key.clone(), value.clone()));

163-

}

160+

let result = env.iter().map(|(key, value)| (key.clone(), value.clone())).collect::<Vec<_>>();

164161165-

return Env { iter: result.into_iter() };

166-

}

162+

Env { iter: result.into_iter() }

167163

}

168164169165

pub fn getenv(k: &OsStr) -> Option<OsString> {

170-

unsafe { ENV.as_ref().unwrap().lock().unwrap().get_mut(k).cloned() }

166+

ENV.lock().unwrap().as_ref().unwrap().get(k).cloned()

171167

}

172168173169

pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {

174170

let (k, v) = (k.to_owned(), v.to_owned());

175-

ENV.as_ref().unwrap().lock().unwrap().insert(k, v);

171+

ENV.lock().unwrap().as_mut().unwrap().insert(k, v);

176172

Ok(())

177173

}

178174179175

pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {

180-

ENV.as_ref().unwrap().lock().unwrap().remove(k);

176+

ENV.lock().unwrap().as_mut().unwrap().remove(k);

181177

Ok(())

182178

}

183179

@@ -190,9 +186,7 @@ pub fn home_dir() -> Option<PathBuf> {

190186

}

191187192188

pub fn exit(code: i32) -> ! {

193-

unsafe {

194-

hermit_abi::exit(code);

195-

}

189+

unsafe { hermit_abi::exit(code) }

196190

}

197191198192

pub fn getpid() -> u32 {