Skip to content

Commit 2093d0c

Browse files
committed
Reformulate std::env::{set,remove}_env as safety note
1 parent 8057586 commit 2093d0c

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

library/std/src/env.rs

+36-24
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,24 @@ impl Error for VarError {
313313
/// Sets the environment variable `key` to the value `value` for the currently running
314314
/// process.
315315
///
316-
/// Note that while concurrent access to environment variables ought to be safe
317-
/// in Rust, some platforms only expose inherently unsafe non-threadsafe APIs
318-
/// for inspecting the environment. As a result, using `set_var` or
319-
/// `remove_var` in a multi-threaded Rust program can lead to undefined
320-
/// behavior, for example in combination with DNS lookups from
321-
/// [`std::net::ToSocketAddrs`]. This is a bug
322-
/// ([rust#27970](https://github.com/rust-lang/rust/issues/27970)) and will be
323-
/// fixed in a future version of Rust. Additionally, extra care needs to be
324-
/// taken when auditing calls to unsafe external FFI functions to ensure that
325-
/// any external environment accesses are properly synchronized with accesses
326-
/// in Rust. Since Rust does not expose its environment lock directly, this
327-
/// means that all accesses to the environment must go through Rust's [`var`].
316+
/// # Safety
317+
///
318+
/// Even though this function is currently not marked as `unsafe`, it needs to
319+
/// be because invoking it can cause undefined behaviour. The function will be
320+
/// marked `unsafe` in a future version of Rust. This is tracked in
321+
/// [rust#27970](https://github.com/rust-lang/rust/issues/27970).
322+
///
323+
/// This function is safe to call in a single-threaded program.
324+
///
325+
/// In multi-threaded programs, you must ensure that are no other threads
326+
/// concurrently writing or *reading*(!) from the environment through functions
327+
/// other than the ones in this module. You are responsible for figuring out
328+
/// how to achieve this, but we strongly suggest not using `set_var` or
329+
/// `remove_var` in multi-threaded programs at all.
330+
///
331+
/// Most C libraries, including libc itself do not advertise which functions
332+
/// read from the environment. Even functions from the Rust standard library do
333+
/// that, e.g. for DNS lookups from [`std::net::ToSocketAddrs`].
328334
///
329335
/// Discussion of this unsafety on Unix may be found in:
330336
///
@@ -360,18 +366,24 @@ fn _set_var(key: &OsStr, value: &OsStr) {
360366

361367
/// Removes an environment variable from the environment of the currently running process.
362368
///
363-
/// Note that while concurrent access to environment variables ought to be safe
364-
/// in Rust, some platforms only expose inherently unsafe non-threadsafe APIs
365-
/// for inspecting the environment. As a result, using `set_var` or
366-
/// `remove_var` in a multi-threaded Rust program can lead to undefined
367-
/// behavior, for example in combination with DNS lookups from
368-
/// [`std::net::ToSocketAddrs`]. This is a bug
369-
/// ([rust#27970](https://github.com/rust-lang/rust/issues/27970)) and will be
370-
/// fixed in a future version of Rust. Additionally, extra care needs to be
371-
/// taken when auditing calls to unsafe external FFI functions to ensure that
372-
/// any external environment accesses are properly synchronized with accesses
373-
/// in Rust. Since Rust does not expose its environment lock directly, this
374-
/// means that all accesses to the environment must go through Rust's [`var`].
369+
/// # Safety
370+
///
371+
/// Even though this function is currently not marked as `unsafe`, it needs to
372+
/// be because invoking it can cause undefined behaviour. The function will be
373+
/// marked `unsafe` in a future version of Rust. This is tracked in
374+
/// [rust#27970](https://github.com/rust-lang/rust/issues/27970).
375+
///
376+
/// This function is safe to call in a single-threaded program.
377+
///
378+
/// In multi-threaded programs, you must ensure that are no other threads
379+
/// concurrently writing or *reading*(!) from the environment through functions
380+
/// other than the ones in this module. You are responsible for figuring out
381+
/// how to achieve this, but we strongly suggest not using `set_var` or
382+
/// `remove_var` in multi-threaded programs at all.
383+
///
384+
/// Most C libraries, including libc itself do not advertise which functions
385+
/// read from the environment. Even functions from the Rust standard library do
386+
/// that, e.g. for DNS lookups from [`std::net::ToSocketAddrs`].
375387
///
376388
/// Discussion of this unsafety on Unix may be found in:
377389
///

0 commit comments

Comments
 (0)