os: use _wputenv instead of _putenv to stay in sync with _wgetenv (fix changing env variables with non ASCII content on windows) (#22920)

This commit is contained in:
Ekopalypse 2024-11-21 00:33:02 +01:00 committed by GitHub
parent b995e64be9
commit ca6727303d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 5 deletions

View file

@ -327,6 +327,7 @@ fn C._wsystem(command &u16) int
fn C._wgetenv(varname &u16) voidptr
fn C._putenv(envstring &char) int
fn C._wputenv(envstring &u16) int
fn C._waccess(path &u16, mode int) int

View file

@ -45,15 +45,18 @@ pub fn getenv_opt(key string) ?string {
// os.setenv sets the value of an environment variable with `name` to `value`.
pub fn setenv(name string, value string, overwrite bool) int {
$if windows {
format := '${name}=${value}'
format := '${name}=${value}'.to_wide()
defer {
unsafe { free(voidptr(format)) }
}
if overwrite {
unsafe {
return C._putenv(&char(format.str))
return C._wputenv(format)
}
} else {
if getenv(name).len == 0 {
unsafe {
return C._putenv(&char(format.str))
return C._wputenv(format)
}
}
}
@ -68,8 +71,11 @@ pub fn setenv(name string, value string, overwrite bool) int {
// os.unsetenv clears an environment variable with `name`.
pub fn unsetenv(name string) int {
$if windows {
format := '${name}='
return C._putenv(&char(format.str))
format := '${name}='.to_wide()
defer {
unsafe { free(voidptr(format)) }
}
return C._wputenv(format)
} $else {
return C.unsetenv(&char(name.str))
}

View file

@ -52,3 +52,19 @@ fn test_getenv_empty_var() {
os.setenv('empty${key}', '""', false)
assert os.getenv('empty${key}') == '""'
}
fn test_environ_non_ascii() {
os.setenv('Büro', 'gebäude', false)
assert os.getenv('Büro') == 'gebäude'
os.setenv('Büro', 'gebäudehaus', true)
assert os.getenv('Büro') == 'gebäudehaus'
os.setenv('Büro', 'gebäudehaus in der Straße', true)
assert os.getenv('Büro') == 'gebäudehaus in der Straße'
os.unsetenv('Büro')
assert os.getenv('Büro') == ''
os.setenv('', ' ', false)
assert os.getenv('') == ' '
os.unsetenv('')
assert os.getenv('') == ''
}