5 ms·
> to safely change the environment variables. The best you could do would be to leak memory every time you set new environment variables so that the old pointer
by forrestthewoods 3y ago
> to safely change the environment variables. The best you could do would be to leak memory every time you set new environment variables so that the old pointers don't get invalidated, and that just creates a new problem and reason not to use `setenv()` (that's arguably worse).
Arguably worse? My goodness no.
This is a rare edge case that most programs don’t encounter. Option 1 is to crash and explode and die. Option 2 is to leak tens of bytes.
Leaking tens of bytes is for sure NOT worse than crashing.
- marcosdumay 3y ago> Leaking tens of bytes is for sure NOT worse than crashing. I do really disagree here. The answer is not clear at all. But then, you are mischaracterizing the problem. The issue is not with crashing, you can get plain bad data too, and this is clearly worse than both leaking memory and crashing. Also, the GP is mischaracterizing the options. You don't need to leave the old values around, you can just copy them into userspace memory.
- DSMan195276 3y agoMy reasoning is simple - the issues here can be avoided if you're careful about how you use `setenv()` and `getenv()`, which many programs already are. The memory leak in contrast would never be avoidable regardless of how you use it.
- forrestthewoods 3y agoThe problem with “be careful” is that libraries often want to use the very unsafe API and there is no standard mechanism to expose safety. It’s fundamentally a bad API design. It could be good. But it is not. There’s a reason this problem comes up on HN once or two a year. And don’t even get me started about printf grabbing a mutex for a stupid locale…
- none_to_remain 3y agoYou could even put a note in the man page that `setenv()` will leak memory. Then ten or twenty years from now there will be a blog post about how a currently trendy language/runtime can be manipulated into looping over `setenv()` zillions of times and OOM'ing, and comments about how no one can possibly be expected to read the man page for this horrible footgun, and it's wrong to expect developers to have any idea about what they're doing, give a shit, or pay attention at all.