diff options
| author | sanuk369 <224479491+quackottu@users.noreply.github.com> | 2026-11-22 11:36:11 +0530 |
|---|---|---|
| committer | sanuk369 <224479491+quackottu@users.noreply.github.com> | 2026-11-22 11:36:11 +0530 |
| commit | 232dd31ceb3b4676fd7b04746dd63aa6bb22ffb1 (patch) | |
| tree | cc1331691da6cfeb6ce607eef4b48dc1f989ba3e /runtime/shims/process.cpp | |
| parent | 43d470316ec6cbcd64002bc3792998d9f5745dac (diff) | |
process: the command line parses back to __wargv (backslash rules, ANSI code page)
quote() escaped quotes but ignored CommandLineToArgvW's backslash rules,
so GetCommandLineA/W and __argv disagreed for an argument ending in a
backslash ("C:\My Games\" next" parsed back as 2 arguments, not 3) or
holding a backslash before a quote. A backslash run before a quote, or
before the closing quote, is now doubled.
widen() decoded the arguments as UTF-8, but main.cpp passes
std::filesystem::path::string(), which is ANSI code page text, so cafe
with an accent reached __wargv as caf\uFFFD. It widens from CP_ACP now;
under a UTF-8 activeCodePage that is the same thing.
New process_cmdline_test (ctest process_cmdline) checks that
GetCommandLineW parses back to __wargv.
Diffstat (limited to 'runtime/shims/process.cpp')
| -rw-r--r-- | runtime/shims/process.cpp | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp index e8ec7c34e..381161580 100644 --- a/runtime/shims/process.cpp +++ b/runtime/shims/process.cpp | |||
| @@ -43,17 +43,25 @@ HANDLE g_done = nullptr; | |||
| 43 | std::string quote(const std::string& a) { | 43 | std::string quote(const std::string& a) { |
| 44 | if (!a.empty() && a.find_first_of(" \t\"") == std::string::npos) return a; | 44 | if (!a.empty() && a.find_first_of(" \t\"") == std::string::npos) return a; |
| 45 | std::string out = "\""; | 45 | std::string out = "\""; |
| 46 | size_t slashes = 0; // CommandLineToArgvW: backslashes are literal unless they precede a quote | ||
| 46 | for (char c : a) { | 47 | for (char c : a) { |
| 47 | if (c == '"') out += '\\'; | 48 | if (c == '\\') { |
| 49 | ++slashes; | ||
| 50 | } else { | ||
| 51 | if (c == '"') out.append(slashes + 1, '\\'); | ||
| 52 | slashes = 0; | ||
| 53 | } | ||
| 48 | out += c; | 54 | out += c; |
| 49 | } | 55 | } |
| 50 | return out + "\""; | 56 | return out.append(slashes, '\\') + "\""; // a trailing run is doubled before the closing quote |
| 51 | } | 57 | } |
| 52 | 58 | ||
| 59 | // The narrow arguments are ANSI code page text (main.cpp passes std::filesystem::path::string(), and the CRT's | ||
| 60 | // narrow argv is ACP too); under a UTF-8 activeCodePage CP_ACP is UTF-8 anyway. | ||
| 53 | std::wstring widen(const std::string& s) { | 61 | std::wstring widen(const std::string& s) { |
| 54 | int n = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, nullptr, 0); | 62 | int n = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, nullptr, 0); |
| 55 | std::wstring w(n > 0 ? n - 1 : 0, L'\0'); | 63 | std::wstring w(n > 0 ? n - 1 : 0, L'\0'); |
| 56 | if (n > 1) MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, w.data(), n); | 64 | if (n > 1) MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, w.data(), n); |
| 57 | return w; | 65 | return w; |
| 58 | } | 66 | } |
| 59 | 67 | ||