From ceca195b9ef85064cd3e4e14727e865e9f1885fe Mon Sep 17 00:00:00 2001 From: Wei-Lun Li Date: Sun, 6 Dec 2026 10:34:44 +0900 Subject: [PATCH] Read environment variables as UTF-8 on Windows (#3713) On Windows, $ENV and env were built from environ, which the C library fills in the ANSI code page, so names and values outside of it were mangled before jq saw them. Build the environment object from GetEnvironmentStringsW() and convert each entry from UTF-16 to UTF-8, skipping the hidden per-drive working directory entries starting with '=' as environ does, and skipping any entry that fails to convert. env and $ENV now share a single reader, get_env() in util.c, which also skips malformed entries without '=' in both, instead of env mapping them to null and $ENV deleting an existing variable of that name. Other platforms still read environ. --- src/builtin.c | 17 +-------------- src/compile.c | 17 +-------------- src/util.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 1 + tests/shtest | 4 ++++ 5 files changed, 65 insertions(+), 32 deletions(-) diff --git a/src/builtin.c b/src/builtin.c index 612b8690..d4c4c986 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -1304,24 +1304,9 @@ static jv f_error(jq_state *jq, jv input) { return jv_invalid_with_msg(input); } -// FIXME Should autoconf check for this! -#ifndef WIN32 -extern char **environ; -#endif - static jv f_env(jq_state *jq, jv input) { jv_free(input); - jv env = jv_object(); - const char *var, *val; - for (char **e = environ; *e != NULL; e++) { - var = e[0]; - val = strchr(e[0], '='); - if (val == NULL) - env = jv_object_set(env, jv_string(var), jv_null()); - else if (val - var < INT_MAX) - env = jv_object_set(env, jv_string_sized(var, val - var), jv_string(val + 1)); - } - return env; + return get_env(); } static jv f_halt(jq_state *jq, jv input) { diff --git a/src/compile.c b/src/compile.c index c800ce81..c17dc560 100644 --- a/src/compile.c +++ b/src/compile.c @@ -1108,27 +1108,12 @@ static int count_cfunctions(block b) { return n; } -#ifndef WIN32 -extern char **environ; -#endif - static jv make_env(jv env) { if (jv_is_valid(env)) return jv_copy(env); - jv r = jv_object(); - if (environ == NULL) - return r; - for (size_t i = 0; environ[i] != NULL; i++) { - const char *eq; - - if ((eq = strchr(environ[i], '=')) == NULL) - r = jv_object_delete(r, jv_string(environ[i])); - else - r = jv_object_set(r, jv_string_sized(environ[i], eq - environ[i]), jv_string(eq + 1)); - } - return jv_copy(r); + return jv_copy(get_env()); } // Expands call instructions into a calling sequence diff --git a/src/util.c b/src/util.c index 2c756354..5616e1f9 100644 --- a/src/util.c +++ b/src/util.c @@ -99,6 +99,64 @@ jv expand_path(jv path) { return ret; } +#ifdef WIN32 +/* + * The C library hands out the environment, through environ and getenv() + * alike, in the ANSI code page, so anything outside of it comes out mangled. + * Read the wide character environment instead and convert it to UTF-8, as + * wmain() does with the arguments. + */ +static char *utf16_to_utf8(const wchar_t *ws) { + int len = WideCharToMultiByte(CP_UTF8, 0, ws, -1, NULL, 0, NULL, NULL); + if (len <= 0) + return NULL; + char *s = jv_mem_alloc(len); + if (WideCharToMultiByte(CP_UTF8, 0, ws, -1, s, len, NULL, NULL) != len) { + jv_mem_free(s); + return NULL; + } + return s; +} +#else +// FIXME Should autoconf check for this! +extern char **environ; +#endif + +static jv env_set(jv env, const char *var) { + const char *val = strchr(var, '='); + if (val != NULL && val - var < INT_MAX) + env = jv_object_set(env, jv_string_sized(var, val - var), jv_string(val + 1)); + return env; +} + +/* The environment as an object, for `env` and `$ENV` */ +jv get_env(void) { + jv env = jv_object(); +#ifdef WIN32 + wchar_t *block = GetEnvironmentStringsW(); + if (block == NULL) + return env; + for (const wchar_t *e = block; *e != L'\0'; e += wcslen(e) + 1) { + /* Skip the hidden per-drive working directories ("=C:=C:\foo"), which + * environ leaves out as well. */ + if (*e == L'=') + continue; + char *var = utf16_to_utf8(e); + if (var == NULL) + continue; + env = env_set(env, var); + jv_mem_free(var); + } + FreeEnvironmentStringsW(block); +#else + if (environ == NULL) + return env; + for (char **e = environ; *e != NULL; e++) + env = env_set(env, *e); +#endif + return env; +} + jv get_home(void) { jv ret; char *home = getenv("HOME"); diff --git a/src/util.h b/src/util.h index a2db598f..b634800e 100644 --- a/src/util.h +++ b/src/util.h @@ -16,6 +16,7 @@ #include "jv.h" jv expand_path(jv); +jv get_env(void); jv get_home(void); jv jq_realpath(jv); diff --git a/tests/shtest b/tests/shtest index 5d177d28..79e46536 100755 --- a/tests/shtest +++ b/tests/shtest @@ -70,6 +70,10 @@ $VALGRIND $Q $JQ -Rne '[inputs] == ["[{\"foo\": 1}", ",{\"foo\": 2}", "]", "[{\" $VALGRIND $Q $JQ -ne --slurpfile x $d/bom1.json '$x == [[{"foo":1},{"foo":2}]]' $VALGRIND $Q $JQ -ne --rawfile x $d/bom1.json '$x == "[{\"foo\": 1}\n,{\"foo\": 2}\n]\n"' +## Test non-ASCII environment variables (Windows has them in UTF-16) +JQ_ENV_TEST=$(printf '\303\251\346\227\245') \ + $VALGRIND $Q $JQ -ne '$ENV.JQ_ENV_TEST == "é日" and env.JQ_ENV_TEST == "é日"' + ## Test that --run-tests reports failures (#2765) cat > $d/passing.test <<'EOF' .