News for hackers. Stuff that matters.
Re:C stdlib isn't threadsafe and even safe Rust didn't save us by belter on Thursday January 23, 2025 @06:23AM (#42802952)
> It seems like the only reliable way to fix this is to change these functions so that they exclusively acquire a mutex.

A mutex can ensure thread safety but risks deadlocks if not used carefully and will hurt performance...

Re:C stdlib isn't threadsafe and even safe Rust didn't save us by hamandcheese on Thursday January 23, 2025 @06:29AM (#42802976)
Agree about performance, but wouldn't there need to be >1 mutex to risk a deadlock?
Re:C stdlib isn't threadsafe and even safe Rust didn't save us by pixelesque on Thursday January 23, 2025 @08:44AM (#42803914)
If it's not a "recursive mutex" (where you can call lock within the same thread on the same mutex more than once consecutively and it handled that), it's possible to lock on itself again (say in code which is recursive)...
Re:C stdlib isn't threadsafe and even safe Rust didn't save us by loeg on Thursday January 23, 2025 @12:30PM (#42805977)
Imagine you get a signal during getenv itself with the mutex held. Then your signal handler calls getenv. (On the other hand -- getenv is not marked async-signal-safe, so this use is already illegal.)
Re:C stdlib isn't threadsafe and even safe Rust didn't save us by fch42 on Thursday January 23, 2025 @12:14PM (#42805829)
The problem (with get/set/putenv as they are) was isn't the non-use of a mutex. It's the "meaning" of the pointer returned to by getenv(). It returns a char*. Nevermind the persistance of that value - you can work around that by deliberately leaking memory - but it's writeable. Whether it's a good idea to do so ... well. But simply locking "inside" these funcs doesn't solve all the / your issues.