WSL: Subprocess cmd.exe with /U to output UTF-16LE#6717
WSL: Subprocess cmd.exe with /U to output UTF-16LE#6717holmanb merged 4 commits intocanonical:mainfrom
Conversation
Peasant comment fix: /init is now open source (as part of WSL2). Fixes: canonical#6716
That function can now throw UnicodeDecodeError, which inherits from ValueError, so we should catch ValueError as before.
|
Thanks for this contribution @CarlosNihelton! A couple of requests:
Also, for my own understanding, I would like to know what environment variables are set by the calling processes for both ds-identify and cloud-init's Python code. Could I ask you to instrument each of these and sharing the results? In ds-identify something like |
|
Hi @holmanb! Here are the logs from a system with username containing non-ascii characters and with the datasource and ds-identify patched: cloud-init.tar.gz Regarding adding coverage to ds-identify I need to ask you a deeper question. AFAICT to make this changeset testable I'd need to break this assignment into two lines: So, I need something like With the diff --git a/tools/ds-identify b/tools/ds-identify
index c2a6d69ea..e4b8507ea 100755
--- a/tools/ds-identify
+++ b/tools/ds-identify
@@ -1710,7 +1710,7 @@ WSL_run_cmd() {
shift
# Using the '/u' flag to enforce Unicode (UTF-16 LE), thus we need to decode it afterwards.
# It's more reliable than the default ANSI Code Pages for anything above the ASCII range.
- _RET=$(/init "$exepath" /u /c "$@" 2>/dev/null | iconv -f UTF-16LE -t UTF-8)
+ _RET=$(/init "$exepath" /u /c "$@" 2>/dev/null | base64)
}
WSL_profile_dir() {
@@ -1725,6 +1725,7 @@ WSL_profile_dir() {
# to output the Windows user profile directory path, which is
# held by the environment variable %USERPROFILE%.
WSL_run_cmd "$cmdexe" "echo.%USERPROFILE%"
- profiledir="${_RET%%[[:cntrl:]]}"
+ profiledir=$(echo $_RET | base64 -d | iconv -f UTF-16LE -t UTF-8)
+ profiledir="${profiledir%%[[:cntrl:]]}"
if [ -n "$profiledir" ]; then
# wslpath is a program supplied by WSL itself that translates Windows and Linux paths,WDYT? |
holmanb
left a comment
There was a problem hiding this comment.
Thanks for proposing some options @CarlosNihelton. Regarding unit testing, I'd say lets leave it as-is for now. Cloud-init's unit test code for ds-identify doesn't support the kind of testing I was hoping for, and you've raised some good points about the complexity - I'd rather not introduce even more complexity to the code for the sake of this test.
How /init is used to launch Windows binaries.
|
Hello! Thank you for this proposed change to cloud-init. This pull request is now marked as stale as it has not seen any activity in 14 days. If no activity occurs within the next 7 days, this pull request will automatically close. If you are waiting for code review and you are seeing this message, apologies! Please reply, tagging blackboxsw, and he will ensure that someone takes a look soon. (If the pull request is closed and you would like to continue working on it, please do tag blackboxsw to reopen it.) |
As we manipulate paths acquired by subprocessing cmd.exe inside WSL, by using it in UTF-16 mode we ensure a predictable output when the strings are not ASCII-compatible, such as reading the user profile when it contains special characters. Fixes canonicalGH-6716
Fixes: #6716
Including a small peasant fix in a comment about the WSL2
/initbeing proprietary (no longer the case since WSL2 was open sourced last year).And using the
echo.syntax instead ofechoto prevent a very unlikely corner case of the environment variable set to white spaces:Proposed Commit Message
Additional Context
Test Steps
Merge type