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/test/process_cmdline_test.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/test/process_cmdline_test.cpp')
| -rw-r--r-- | runtime/test/process_cmdline_test.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp new file mode 100644 index 000000000..0df7b4e86 --- /dev/null +++ b/runtime/test/process_cmdline_test.cpp | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | // The process shim's command line round-trips: what GetCommandLineW says must parse with CommandLineToArgvW (the | ||
| 2 | // documented rules the CRT's argv parser shares) back to the __wargv the shim hands out, and __wargv must hold what | ||
| 3 | // main.cpp passed. No game files. | ||
| 4 | #include <windows.h> | ||
| 5 | #include <shellapi.h> | ||
| 6 | |||
| 7 | #include <cstdio> | ||
| 8 | #include <filesystem> | ||
| 9 | #include <string> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "process_shim.h" | ||
| 13 | #include "shims.h" | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | int failures = 0; | ||
| 17 | |||
| 18 | wchar_t** wargv() { | ||
| 19 | using Fn = wchar_t*** (__cdecl*)(); | ||
| 20 | return *reinterpret_cast<Fn>(rt::shims::find("ucrtbase.dll", "__p___wargv"))(); | ||
| 21 | } | ||
| 22 | |||
| 23 | void round_trip(const std::vector<std::string>& args, const char* what) { | ||
| 24 | rt::process::set_command_line("game.exe", args); | ||
| 25 | using Fn = LPWSTR(WINAPI*)(); | ||
| 26 | const LPWSTR line = reinterpret_cast<Fn>(rt::shims::find("kernel32.dll", "GetCommandLineW"))(); | ||
| 27 | int count = 0; | ||
| 28 | LPWSTR* parsed = CommandLineToArgvW(line, &count); | ||
| 29 | bool ok = parsed && count == int(args.size()) + 1; | ||
| 30 | for (int i = 0; ok && i < count; ++i) ok = wargv()[i] && wcscmp(parsed[i], wargv()[i]) == 0; | ||
| 31 | if (!ok) { | ||
| 32 | std::fwprintf(stderr, L"FAIL %hs: GetCommandLineW [%ls] parses to %d argument(s)\n", what, line, count); | ||
| 33 | ++failures; | ||
| 34 | } | ||
| 35 | LocalFree(parsed); | ||
| 36 | } | ||
| 37 | } // namespace | ||
| 38 | |||
| 39 | int main() { | ||
| 40 | round_trip({"plain", "with space"}, "plain and spaced arguments"); | ||
| 41 | round_trip({"C:\\My Games\\", "next"}, "a quoted argument ending in a backslash"); | ||
| 42 | round_trip({"a\\\"b"}, "a backslash before a quote"); | ||
| 43 | round_trip({"\\\\server\\share\\x y"}, "a UNC path with a space"); | ||
| 44 | |||
| 45 | // main.cpp passes game arguments as std::filesystem::path::string(): ANSI code page text, not UTF-8. | ||
| 46 | const std::wstring cafe = L"caf\u00e9"; | ||
| 47 | round_trip({std::filesystem::path(cafe).string()}, "a non-ASCII argument"); | ||
| 48 | if (!wargv()[1] || cafe != wargv()[1]) { | ||
| 49 | std::fprintf(stderr, "FAIL: __wargv[1] is not L\"caf\\u00e9\"\n"); | ||
| 50 | ++failures; | ||
| 51 | } | ||
| 52 | |||
| 53 | if (failures) return 1; | ||
| 54 | std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact\n"); | ||
| 55 | return 0; | ||
| 56 | } | ||