From fbdace5edd111a6666f6bbf30c2daa72c351780b Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 11 Jan 2022 13:01:05 +0100 Subject: [PATCH 1/2] Fix GH-7896: Environment vars may be mangled on Windows When bug 77574[1] has been fixed, the fix only catered to variables retrieved via `getenv()` with a `$varname` passed, but neither to `getenv()` without arguments nor to the general import of environment variables into `$_ENV` and `$_SERVER`. We catch up on this by using `GetEnvironmentStringsW()` in `_php_import_environment_variables()` and converting the encoding to whatever had been chosen by the user. [1] --- main/php_variables.c | 12 ++++++++---- tests/basic/gh7896.phpt | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 tests/basic/gh7896.phpt diff --git a/main/php_variables.c b/main/php_variables.c index 312c22ef07db7..6a8ff5e1f0910 100644 --- a/main/php_variables.c +++ b/main/php_variables.c @@ -571,11 +571,15 @@ void _php_import_environment_variables(zval *array_ptr) import_environment_variable(Z_ARRVAL_P(array_ptr), *env); } #else - char *environment = GetEnvironmentStringsA(); - for (char *env = environment; env != NULL && *env; env += strlen(env) + 1) { - import_environment_variable(Z_ARRVAL_P(array_ptr), env); + wchar_t *environmentw = GetEnvironmentStringsW(); + for (wchar_t *envw = environmentw; envw != NULL && *envw; envw += wcslen(envw) + 1) { + char *env = php_win32_cp_w_to_any(envw); + if (env != NULL) { + import_environment_variable(Z_ARRVAL_P(array_ptr), env); + free(env); + } } - FreeEnvironmentStringsA(environment); + FreeEnvironmentStringsW(environmentw); #endif tsrm_env_unlock(); diff --git a/tests/basic/gh7896.phpt b/tests/basic/gh7896.phpt new file mode 100644 index 0000000000000..af6e9edba4af3 --- /dev/null +++ b/tests/basic/gh7896.phpt @@ -0,0 +1,16 @@ +--TEST-- +GH-7896 (Environment vars may be mangled on Windows) +--ENV-- +FÖÖ=GüИter传 +--FILE-- + +--EXPECT-- +string(11) "GüИter传" +string(11) "GüИter传" +string(11) "GüИter传" From 72a8f4121d46a07bc97d531780879f2d9388e240 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 11 Jan 2022 13:43:42 +0100 Subject: [PATCH 2/2] Skip test unless on Windows Apparently, other systems may not properly work with environment variable names which contain non ASCII characters, so we skip the test there. --- tests/basic/gh7896.phpt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/basic/gh7896.phpt b/tests/basic/gh7896.phpt index af6e9edba4af3..2ad2958d1cd9a 100644 --- a/tests/basic/gh7896.phpt +++ b/tests/basic/gh7896.phpt @@ -1,5 +1,9 @@ --TEST-- GH-7896 (Environment vars may be mangled on Windows) +--SKIPIF-- + --ENV-- FÖÖ=GüИter传 --FILE--