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.
This commit is contained in:
Wei-Lun Li authored and GitHub committed 2026-12-06 10:34:44 +09:00
1 parent 22c22bcc30
commit ceca195b9e
5 files changed
+65 -32

No files matched your search

+4
View File
@@ -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'
.