Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c4b1ff8

Browse files
committedMay 29, 2024
Make std::env::{set_var, remove_var} unsafe in edition 2024
Allow calling these functions without `unsafe` blocks in editions up until 2021, but don't trigger the `unused_unsafe` lint for `unsafe` blocks containing these functions. Fixes rust-lang#27970. Fixes rust-lang#90308. CC rust-lang#124866.
1 parent 1aaf0a9 commit c4b1ff8

File tree

12 files changed

+63
-53
lines changed

12 files changed

+63
-53
lines changed
 

‎std/src/env.rs

+34-20
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,6 @@ impl Error for VarError {
318318
///
319319
/// # Safety
320320
///
321-
/// Even though this function is currently not marked as `unsafe`, it needs to
322-
/// be because invoking it can cause undefined behaviour. The function will be
323-
/// marked `unsafe` in a future version of Rust. This is tracked in
324-
/// [rust#27970](https://github.com/rust-lang/rust/issues/27970).
325-
///
326321
/// This function is safe to call in a single-threaded program.
327322
///
328323
/// In multi-threaded programs, you must ensure that are no other threads
@@ -331,7 +326,7 @@ impl Error for VarError {
331326
/// how to achieve this, but we strongly suggest not using `set_var` or
332327
/// `remove_var` in multi-threaded programs at all.
333328
///
334-
/// Most C libraries, including libc itself do not advertise which functions
329+
/// Most C libraries, including libc itself, do not advertise which functions
335330
/// read from the environment. Even functions from the Rust standard library do
336331
/// that, e.g. for DNS lookups from [`std::net::ToSocketAddrs`].
337332
///
@@ -353,15 +348,26 @@ impl Error for VarError {
353348
/// use std::env;
354349
///
355350
/// let key = "KEY";
356-
/// env::set_var(key, "VALUE");
351+
/// unsafe {
352+
/// env::set_var(key, "VALUE");
353+
/// }
357354
/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
358355
/// ```
356+
#[cfg(not(bootstrap))]
357+
#[rustc_deprecated_safe_2024]
359358
#[stable(feature = "env", since = "1.0.0")]
360-
pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
359+
pub unsafe fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
361360
_set_var(key.as_ref(), value.as_ref())
362361
}
363362

364-
fn _set_var(key: &OsStr, value: &OsStr) {
363+
#[cfg(bootstrap)]
364+
#[allow(missing_docs)]
365+
#[stable(feature = "env", since = "1.0.0")]
366+
pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
367+
unsafe { _set_var(key.as_ref(), value.as_ref()) }
368+
}
369+
370+
unsafe fn _set_var(key: &OsStr, value: &OsStr) {
365371
os_imp::setenv(key, value).unwrap_or_else(|e| {
366372
panic!("failed to set environment variable `{key:?}` to `{value:?}`: {e}")
367373
})
@@ -371,11 +377,6 @@ fn _set_var(key: &OsStr, value: &OsStr) {
371377
///
372378
/// # Safety
373379
///
374-
/// Even though this function is currently not marked as `unsafe`, it needs to
375-
/// be because invoking it can cause undefined behaviour. The function will be
376-
/// marked `unsafe` in a future version of Rust. This is tracked in
377-
/// [rust#27970](https://github.com/rust-lang/rust/issues/27970).
378-
///
379380
/// This function is safe to call in a single-threaded program.
380381
///
381382
/// In multi-threaded programs, you must ensure that are no other threads
@@ -384,7 +385,7 @@ fn _set_var(key: &OsStr, value: &OsStr) {
384385
/// how to achieve this, but we strongly suggest not using `set_var` or
385386
/// `remove_var` in multi-threaded programs at all.
386387
///
387-
/// Most C libraries, including libc itself do not advertise which functions
388+
/// Most C libraries, including libc itself, do not advertise which functions
388389
/// read from the environment. Even functions from the Rust standard library do
389390
/// that, e.g. for DNS lookups from [`std::net::ToSocketAddrs`].
390391
///
@@ -403,22 +404,35 @@ fn _set_var(key: &OsStr, value: &OsStr) {
403404
///
404405
/// # Examples
405406
///
406-
/// ```
407+
/// ```no_run
407408
/// use std::env;
408409
///
409410
/// let key = "KEY";
410-
/// env::set_var(key, "VALUE");
411+
/// unsafe {
412+
/// env::set_var(key, "VALUE");
413+
/// }
411414
/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
412415
///
413-
/// env::remove_var(key);
416+
/// unsafe {
417+
/// env::remove_var(key);
418+
/// }
414419
/// assert!(env::var(key).is_err());
415420
/// ```
421+
#[cfg(not(bootstrap))]
422+
#[rustc_deprecated_safe_2024]
416423
#[stable(feature = "env", since = "1.0.0")]
417-
pub fn remove_var<K: AsRef<OsStr>>(key: K) {
424+
pub unsafe fn remove_var<K: AsRef<OsStr>>(key: K) {
418425
_remove_var(key.as_ref())
419426
}
420427

421-
fn _remove_var(key: &OsStr) {
428+
#[cfg(bootstrap)]
429+
#[allow(missing_docs)]
430+
#[stable(feature = "env", since = "1.0.0")]
431+
pub fn remove_var<K: AsRef<OsStr>>(key: K) {
432+
unsafe { _remove_var(key.as_ref()) }
433+
}
434+
435+
unsafe fn _remove_var(key: &OsStr) {
422436
os_imp::unsetenv(key)
423437
.unwrap_or_else(|e| panic!("failed to remove environment variable `{key:?}`: {e}"))
424438
}

‎std/src/sys/pal/hermit/os.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,14 @@ pub fn getenv(k: &OsStr) -> Option<OsString> {
172172
unsafe { ENV.as_ref().unwrap().lock().unwrap().get_mut(k).cloned() }
173173
}
174174

175-
pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
176-
unsafe {
177-
let (k, v) = (k.to_owned(), v.to_owned());
178-
ENV.as_ref().unwrap().lock().unwrap().insert(k, v);
179-
}
175+
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
176+
let (k, v) = (k.to_owned(), v.to_owned());
177+
ENV.as_ref().unwrap().lock().unwrap().insert(k, v);
180178
Ok(())
181179
}
182180

183-
pub fn unsetenv(k: &OsStr) -> io::Result<()> {
184-
unsafe {
185-
ENV.as_ref().unwrap().lock().unwrap().remove(k);
186-
}
181+
pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {
182+
ENV.as_ref().unwrap().lock().unwrap().remove(k);
187183
Ok(())
188184
}
189185

‎std/src/sys/pal/sgx/os.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ pub fn getenv(k: &OsStr) -> Option<OsString> {
157157
get_env_store().and_then(|s| s.lock().unwrap().get(k).cloned())
158158
}
159159

160-
pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
160+
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
161161
let (k, v) = (k.to_owned(), v.to_owned());
162162
create_env_store().lock().unwrap().insert(k, v);
163163
Ok(())
164164
}
165165

166-
pub fn unsetenv(k: &OsStr) -> io::Result<()> {
166+
pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {
167167
if let Some(env) = get_env_store() {
168168
env.lock().unwrap().remove(k);
169169
}

‎std/src/sys/pal/solid/os.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub fn getenv(k: &OsStr) -> Option<OsString> {
191191
.flatten()
192192
}
193193

194-
pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
194+
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
195195
run_with_cstr(k.as_bytes(), &|k| {
196196
run_with_cstr(v.as_bytes(), &|v| {
197197
let _guard = ENV_LOCK.write();
@@ -200,7 +200,7 @@ pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
200200
})
201201
}
202202

203-
pub fn unsetenv(n: &OsStr) -> io::Result<()> {
203+
pub unsafe fn unsetenv(n: &OsStr) -> io::Result<()> {
204204
run_with_cstr(n.as_bytes(), &|nbuf| {
205205
let _guard = ENV_LOCK.write();
206206
cvt_env(unsafe { libc::unsetenv(nbuf.as_ptr()) }).map(drop)

‎std/src/sys/pal/teeos/os.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ pub fn getenv(_: &OsStr) -> Option<OsString> {
109109
None
110110
}
111111

112-
pub fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
112+
pub unsafe fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
113113
Err(io::Error::new(io::ErrorKind::Unsupported, "cannot set env vars on this platform"))
114114
}
115115

116-
pub fn unsetenv(_: &OsStr) -> io::Result<()> {
116+
pub unsafe fn unsetenv(_: &OsStr) -> io::Result<()> {
117117
Err(io::Error::new(io::ErrorKind::Unsupported, "cannot unset env vars on this platform"))
118118
}
119119

‎std/src/sys/pal/uefi/os.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ pub fn getenv(_: &OsStr) -> Option<OsString> {
203203
None
204204
}
205205

206-
pub fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
206+
pub unsafe fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
207207
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot set env vars on this platform"))
208208
}
209209

210-
pub fn unsetenv(_: &OsStr) -> io::Result<()> {
210+
pub unsafe fn unsetenv(_: &OsStr) -> io::Result<()> {
211211
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot unset env vars on this platform"))
212212
}
213213

‎std/src/sys/pal/unix/os.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -675,19 +675,19 @@ pub fn getenv(k: &OsStr) -> Option<OsString> {
675675
.flatten()
676676
}
677677

678-
pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
678+
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
679679
run_with_cstr(k.as_bytes(), &|k| {
680680
run_with_cstr(v.as_bytes(), &|v| {
681681
let _guard = ENV_LOCK.write();
682-
cvt(unsafe { libc::setenv(k.as_ptr(), v.as_ptr(), 1) }).map(drop)
682+
cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(drop)
683683
})
684684
})
685685
}
686686

687-
pub fn unsetenv(n: &OsStr) -> io::Result<()> {
687+
pub unsafe fn unsetenv(n: &OsStr) -> io::Result<()> {
688688
run_with_cstr(n.as_bytes(), &|nbuf| {
689689
let _guard = ENV_LOCK.write();
690-
cvt(unsafe { libc::unsetenv(nbuf.as_ptr()) }).map(drop)
690+
cvt(libc::unsetenv(nbuf.as_ptr())).map(drop)
691691
})
692692
}
693693

‎std/src/sys/pal/unsupported/os.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ pub fn getenv(_: &OsStr) -> Option<OsString> {
9696
None
9797
}
9898

99-
pub fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
99+
pub unsafe fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
100100
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot set env vars on this platform"))
101101
}
102102

103-
pub fn unsetenv(_: &OsStr) -> io::Result<()> {
103+
pub unsafe fn unsetenv(_: &OsStr) -> io::Result<()> {
104104
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot unset env vars on this platform"))
105105
}
106106

‎std/src/sys/pal/wasi/os.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ pub fn getenv(k: &OsStr) -> Option<OsString> {
244244
.flatten()
245245
}
246246

247-
pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
247+
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
248248
run_with_cstr(k.as_bytes(), &|k| {
249249
run_with_cstr(v.as_bytes(), &|v| unsafe {
250250
let _guard = env_write_lock();
@@ -253,7 +253,7 @@ pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
253253
})
254254
}
255255

256-
pub fn unsetenv(n: &OsStr) -> io::Result<()> {
256+
pub unsafe fn unsetenv(n: &OsStr) -> io::Result<()> {
257257
run_with_cstr(n.as_bytes(), &|nbuf| unsafe {
258258
let _guard = env_write_lock();
259259
cvt(libc::unsetenv(nbuf.as_ptr())).map(drop)

‎std/src/sys/pal/windows/os.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,16 @@ pub fn getenv(k: &OsStr) -> Option<OsString> {
302302
.ok()
303303
}
304304

305-
pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
305+
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
306306
let k = to_u16s(k)?;
307307
let v = to_u16s(v)?;
308308

309-
cvt(unsafe { c::SetEnvironmentVariableW(k.as_ptr(), v.as_ptr()) }).map(drop)
309+
cvt(c::SetEnvironmentVariableW(k.as_ptr(), v.as_ptr())).map(drop)
310310
}
311311

312-
pub fn unsetenv(n: &OsStr) -> io::Result<()> {
312+
pub unsafe fn unsetenv(n: &OsStr) -> io::Result<()> {
313313
let v = to_u16s(n)?;
314-
cvt(unsafe { c::SetEnvironmentVariableW(v.as_ptr(), ptr::null()) }).map(drop)
314+
cvt(c::SetEnvironmentVariableW(v.as_ptr(), ptr::null())).map(drop)
315315
}
316316

317317
pub fn temp_dir() -> PathBuf {

‎std/src/sys/pal/xous/os.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ pub fn getenv(_: &OsStr) -> Option<OsString> {
149149
None
150150
}
151151

152-
pub fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
152+
pub unsafe fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
153153
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot set env vars on this platform"))
154154
}
155155

156-
pub fn unsetenv(_: &OsStr) -> io::Result<()> {
156+
pub unsafe fn unsetenv(_: &OsStr) -> io::Result<()> {
157157
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot unset env vars on this platform"))
158158
}
159159

‎std/src/sys/pal/zkvm/os.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ pub fn getenv(varname: &OsStr) -> Option<OsString> {
115115
Some(OsString::from_inner(os_str::Buf { inner: u8s.to_vec() }))
116116
}
117117

118-
pub fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
118+
pub unsafe fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
119119
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot set env vars on this platform"))
120120
}
121121

122-
pub fn unsetenv(_: &OsStr) -> io::Result<()> {
122+
pub unsafe fn unsetenv(_: &OsStr) -> io::Result<()> {
123123
Err(io::const_io_error!(io::ErrorKind::Unsupported, "cannot unset env vars on this platform"))
124124
}
125125

0 commit comments

Comments
 (0)
Failed to load comments.