Live data from Hacker News

C stdlib isn't threadsafe and even safe Rust didn't save us

edgedb.com

1–10 of 370 posts

Re: C stdlib isn't threadsafe and even safe Rust didn't save us

#4
In the Rust std, `set_var` and `remove_var` will correctly require using an `unsafe {}` block in the next edition (2024). The documentation does now mention the safety issue but obviously it was a mistake to make these functions safe originally (albeit a mistake even higher level languages have made).

https://doc.rust-lang.org/stable/std/env/fn.set_var.html

There is a patch for glibc which makes `getenv` safe in more cases where the environment is modified but C still allows direct access to the environ so it can't be completely safe in the face of modification https://github.com/bminor/glibc/commit/7a61e7f557a97ab597d6f...

Re: C stdlib isn't threadsafe and even safe Rust didn't save us

#5
The major takeaway from this is that Rust will be making environment setters unsafe in the next edition. With luck, this will filter down into crates that trigger these crashes (https://github.com/alexcrichton/openssl-probe/issues/30 filed upstream in the meantime).

Re: C stdlib isn't threadsafe and even safe Rust didn't save us

#6

Mutable global state is evil. Friends don’t let friends use mutable global state. I hate envvars. It’s “the Linux way”. I avoid them like the plague. A++ strong recommend. libc is terrible. The world needs to move on.

Env vars are good if you treat them as read-only within the process

Re: C stdlib isn't threadsafe and even safe Rust didn't save us

#7

Mutable global state is evil. Friends don’t let friends use mutable global state. I hate envvars. It’s “the Linux way”. I avoid them like the plague. A++ strong recommend. libc is terrible. The world needs to move on.

What's your preferred alternative?

Re: C stdlib isn't threadsafe and even safe Rust didn't save us

#8
post #2

Yet another person is burned by calling setenv() in a multi-threaded context. There really needs to be a big warning banner on the manpage for setenv() that warns about this because it seems like a far more common problem than you would expect.

Funny enough, the Rust wrapper `std::env::set_var` does have a big warning https://doc.rust-lang.org/std/env/fn.set_var.html

Re: C stdlib isn't threadsafe and even safe Rust didn't save us

#9
post #2

Yet another person is burned by calling setenv() in a multi-threaded context. There really needs to be a big warning banner on the manpage for setenv() that warns about this because it seems like a far more common problem than you would expect.

The man page says:

> POSIX.1 does not require setenv() or unsetenv() to be reentrant.

A non-reentrant function cannot be thread safe.

In general (for POSIX, libc and many other libraries: if the docs do not explicitly say "this function is thread safe" they are not).

Re: C stdlib isn't threadsafe and even safe Rust didn't save us

#10

Mutable global state is evil. Friends don’t let friends use mutable global state. I hate envvars. It’s “the Linux way”. I avoid them like the plague. A++ strong recommend. libc is terrible. The world needs to move on.

libc moved the world into the Information Age
Post reply on HN