Skip to content

Commit 44fd74a

Browse files
authored
Rollup merge of #116888 - tbu-:pr_unsafe_env, r=Amanieu
Add discussion that concurrent access to the environment is unsafe The bug report #27970 has existed for 8 years, the actual bug dates back to Rust pre-1.0. I documented it since it's in the interest of the user to be aware of it. The note can be removed once #27970 is fixed.
2 parents 604f185 + 2093d0c commit 44fd74a

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

library/std/src/env.rs

+40-10
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,32 @@ 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 is safe in Rust,
317-
/// some platforms only expose inherently unsafe non-threadsafe APIs for
318-
/// inspecting the environment. As a result, extra care needs to be taken when
319-
/// auditing calls to unsafe external FFI functions to ensure that any external
320-
/// environment accesses are properly synchronized with accesses in Rust.
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`].
321334
///
322335
/// Discussion of this unsafety on Unix may be found in:
323336
///
324337
/// - [Austin Group Bugzilla](https://austingroupbugs.net/view.php?id=188)
325338
/// - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2)
326339
///
340+
/// [`std::net::ToSocketAddrs`]: crate::net::ToSocketAddrs
341+
///
327342
/// # Panics
328343
///
329344
/// This function may panic if `key` is empty, contains an ASCII equals sign `'='`
@@ -351,17 +366,32 @@ fn _set_var(key: &OsStr, value: &OsStr) {
351366

352367
/// Removes an environment variable from the environment of the currently running process.
353368
///
354-
/// Note that while concurrent access to environment variables is safe in Rust,
355-
/// some platforms only expose inherently unsafe non-threadsafe APIs for
356-
/// inspecting the environment. As a result extra care needs to be taken when
357-
/// auditing calls to unsafe external FFI functions to ensure that any external
358-
/// environment accesses are properly synchronized with accesses in Rust.
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`].
359387
///
360388
/// Discussion of this unsafety on Unix may be found in:
361389
///
362390
/// - [Austin Group Bugzilla](https://austingroupbugs.net/view.php?id=188)
363391
/// - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2)
364392
///
393+
/// [`std::net::ToSocketAddrs`]: crate::net::ToSocketAddrs
394+
///
365395
/// # Panics
366396
///
367397
/// This function may panic if `key` is empty, contains an ASCII equals sign

0 commit comments

Comments
 (0)