Add lock to Core::SystemInfo::(Get/Set)Environment #2104
Open
nxtum wants to merge 10 commits intordkcentral:masterfrom
Open
Add lock to Core::SystemInfo::(Get/Set)Environment #2104nxtum wants to merge 10 commits intordkcentral:masterfrom
nxtum wants to merge 10 commits intordkcentral:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds synchronization around Core::SystemInfo::{GetEnvironment,SetEnvironment} to avoid unsafe parallel getenv/setenv usage on some libc implementations.
Changes:
- Introduced a file-scope
CriticalSectionintended to protect environment access. - Wrapped Linux
getenv()andsetenv()/unsetenv()calls withSafeSyncType<CriticalSection>.
sebaszm
requested changes
Apr 29, 2026
Contributor
sebaszm
left a comment
There was a problem hiding this comment.
The problem is not only being setenv getenv not thread safe, but also the combination "getenv then setenv" is susceptible to race (so also the widows case needs a lock on set).
Please review the possibility of using getenv_s ( STDC_LIB_EXT1 ).
Also please check the return value of setenv I think it's -1/0 not true/false.
| /* static */ bool SystemInfo::SetEnvironment(const string& name, const TCHAR* value, const bool forced) | ||
| { | ||
| bool result = false; | ||
| SafeSyncType<CriticalSection> scopedLock(_lock); |
Comment on lines
+388
to
+394
| ASSERT(characters < capacity); | ||
| if (characters > 0) { | ||
| value.assign(buffer, characters); | ||
| } else { | ||
| value.clear(); | ||
| } | ||
| return (characters > 0); |
Comment on lines
+388
to
+390
| ASSERT(characters < capacity); | ||
| if (characters > 0) { | ||
| value.assign(buffer, characters); |
sebaszm
requested changes
May 4, 2026
Comment on lines
+388
to
+390
| ASSERT(characters < capacity); | ||
| if (characters > 0) { | ||
| value.assign(buffer, characters); |
| /* static */ bool SystemInfo::SetEnvironment(const string& name, const TCHAR* value, const bool forced) | ||
| { | ||
| bool result = false; | ||
| SafeSyncType<CriticalSection> scopedLock(_lock); |
sebaszm
approved these changes
May 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Not all libc implementation protect parallel usage of getenv and setenv