Skip to content

uucore: centralize unsafe env::set_var/remove_var in a wrapper module#12068

Open
sylvestre wants to merge 1 commit into
uutils:mainfrom
sylvestre:set_var
Open

uucore: centralize unsafe env::set_var/remove_var in a wrapper module#12068
sylvestre wants to merge 1 commit into
uutils:mainfrom
sylvestre:set_var

Conversation

@sylvestre
Copy link
Copy Markdown
Contributor

No description provided.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 28, 2026

GNU testsuite comparison:

Skip an intermittent issue tests/date/resolution (fails in this run but passes in the 'main' branch)
Note: The gnu test tests/rm/many-dir-entries-vs-OOM is now being skipped but was previously passing.
Note: The gnu test tests/tail/tail-n0f is now being skipped but was previously passing.
Congrats! The gnu test tests/tail/pipe-f is now passing!

@sylvestre sylvestre force-pushed the set_var branch 3 times, most recently from 2b3c43d to 14394df Compare April 28, 2026 21:06
@sylvestre sylvestre marked this pull request as ready for review April 28, 2026 21:20
@sylvestre
Copy link
Copy Markdown
Contributor Author

@oech3 wdyt ? :)

@oech3
Copy link
Copy Markdown
Contributor

oech3 commented Apr 28, 2026

This does not make set_var an actual thread safe function...

///
/// Wrapper around [`std::env::set_var`]. See the module documentation for
/// the safety considerations callers must uphold.
pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates Rust's safety model. Safe Rust code must never be able to trigger UB by calling a safe function according to its documented signature. This wrapper makes the function safe to call from anywhere, but does not enforce the required invariants.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure but i don't think it is a big deal. i don't think we have a program dealing with env variable in a parallel context

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dd, sort

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and external crates

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you are overreacting. afaik, it doesn't introduce safety issues

@sylvestre
Copy link
Copy Markdown
Contributor Author

This does not make set_var an actual thread safe function...

of course but it centralized its management

@sylvestre sylvestre requested a review from cakebaker April 30, 2026 09:29
@oech3
Copy link
Copy Markdown
Contributor

oech3 commented May 11, 2026

How about adding unsafe { uucore::env::declare_single_thread() }?

@xtqqczze
Copy link
Copy Markdown
Contributor

How about adding unsafe { uucore::env::declare_single_thread() }?

How would that help? std::env::set_var was made unsafe precisely because there’s no way to guarantee that external C libraries access the environment in a thread-safe manner.

@xtqqczze
Copy link
Copy Markdown
Contributor

I think we should remove use of these functions instead: #12238

@oech3
Copy link
Copy Markdown
Contributor

oech3 commented May 14, 2026

Can we wrap Mutex here?

@xtqqczze
Copy link
Copy Markdown
Contributor

xtqqczze commented May 14, 2026

A mutex doesn't address the soundness issue, see rust-lang/rust#124636 (comment).

cc: @ChrisDenton

@ChrisDenton
Copy link
Copy Markdown
Contributor

In an application if you can be 100% certain there is only one thread then it is safe to mutate the environment because another function literally can't be called while you're using set_var or remove_var.

If there are, or could be, multiple threads then there would need to be some way to guarantee that no thread (directly or indirectly) accesses the environment. This is practically impossible to uphold since many things can read the environment (e.g. almost any libc function reserves the right to do so). A mutex doesn't really help.

@xtqqczze
Copy link
Copy Markdown
Contributor

@sylvestre The proposed uucore::env::set_var and uucore::env::remove_var functions are public APIs, so we can’t make guarantees about threading. Once exposed, they need to remain sound under arbitrary usage, including multi-threaded contexts.

@tbu-
Copy link
Copy Markdown

tbu- commented May 15, 2026

It's a bit weird to "centralize" unsafe functions into a single function not marked as unsafe. It moves the unsafe block away from the place where you need to argue that the usage is actually safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants