1 fix: document env mutation safety · RustPython/RustPython@3e07345 · GitHub
Skip to content

Commit 3e07345

Browse files
Copilotyouknowone
andauthored
fix: document env mutation safety
Agent-Logs-Url: https://github.com/RustPython/RustPython/sessions/d96f57e1-b196-4460-9983-97d5ff118835 Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
1 parent 60cf7dd commit 3e07345

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

crates/common/src/refcount.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ const WEAK_COUNT: usize = 1 << STRONG_WIDTH;
1717
#[cold]
1818
fn refcount_overflow() -> ! {
1919
#[cfg(feature = "std")]
20+
// SAFETY: abort terminates the process immediately and does not return.
2021
unsafe {
2122
libc::abort()
22-
}
23+
};
2324
#[cfg(not(feature = "std"))]
2425
core::panic!("refcount overflow");
2526
}

crates/host_env/src/os.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ pub fn vars_os() -> env::VarsOs {
4848
env::vars_os()
4949
}
5050

51-
pub fn set_var(key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) {
51+
/// # Safety
52+
/// The caller must ensure no other threads can concurrently read or write
53+
/// the process environment while this mutation is performed.
54+
pub unsafe fn set_var(key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) {
5255
unsafe { env::set_var(key, value) };
5356
}
5457

55-
pub fn remove_var(key: impl AsRef<OsStr>) {
58+
/// # Safety
59+
/// The caller must ensure no other threads can concurrently read or write
60+
/// the process environment while this mutation is performed.
61+
pub unsafe fn remove_var(key: impl AsRef<OsStr>) {
5662
unsafe { env::remove_var(key) };
5763
}
5864

crates/vm/src/stdlib/os.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ pub(super) mod _os {
544544
let key = super::bytes_as_os_str(key, vm)?;
545545
let value = super::bytes_as_os_str(value, vm)?;
546546
// SAFETY: requirements forwarded from the caller
547-
crate::host_env::os::set_var(key, value);
547+
unsafe { crate::host_env::os::set_var(key, value) };
548548
Ok(())
549549
}
550550

@@ -596,7 +596,7 @@ pub(super) mod _os {
596596
}
597597
let key = super::bytes_as_os_str(key, vm)?;
598598
// SAFETY: requirements forwarded from the caller
599-
crate::host_env::os::remove_var(key);
599+
unsafe { crate::host_env::os::remove_var(key) };
600600
Ok(())
601601
}
602602

0 commit comments

Comments
 (0)