From 232dd31ceb3b4676fd7b04746dd63aa6bb22ffb1 Mon Sep 17 00:00:00 2001 From: sanuk369 <224479491+quackottu@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:36:11 +0530 Subject: 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. --- runtime/test/process_cmdline_test.cpp | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 runtime/test/process_cmdline_test.cpp (limited to 'runtime/test/process_cmdline_test.cpp') 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 @@ +// The process shim's command line round-trips: what GetCommandLineW says must parse with CommandLineToArgvW (the +// documented rules the CRT's argv parser shares) back to the __wargv the shim hands out, and __wargv must hold what +// main.cpp passed. No game files. +#include +#include + +#include +#include +#include +#include + +#include "process_shim.h" +#include "shims.h" + +namespace { +int failures = 0; + +wchar_t** wargv() { + using Fn = wchar_t*** (__cdecl*)(); + return *reinterpret_cast(rt::shims::find("ucrtbase.dll", "__p___wargv"))(); +} + +void round_trip(const std::vector& args, const char* what) { + rt::process::set_command_line("game.exe", args); + using Fn = LPWSTR(WINAPI*)(); + const LPWSTR line = reinterpret_cast(rt::shims::find("kernel32.dll", "GetCommandLineW"))(); + int count = 0; + LPWSTR* parsed = CommandLineToArgvW(line, &count); + bool ok = parsed && count == int(args.size()) + 1; + for (int i = 0; ok && i < count; ++i) ok = wargv()[i] && wcscmp(parsed[i], wargv()[i]) == 0; + if (!ok) { + std::fwprintf(stderr, L"FAIL %hs: GetCommandLineW [%ls] parses to %d argument(s)\n", what, line, count); + ++failures; + } + LocalFree(parsed); +} +} // namespace + +int main() { + round_trip({"plain", "with space"}, "plain and spaced arguments"); + round_trip({"C:\\My Games\\", "next"}, "a quoted argument ending in a backslash"); + round_trip({"a\\\"b"}, "a backslash before a quote"); + round_trip({"\\\\server\\share\\x y"}, "a UNC path with a space"); + + // main.cpp passes game arguments as std::filesystem::path::string(): ANSI code page text, not UTF-8. + const std::wstring cafe = L"caf\u00e9"; + round_trip({std::filesystem::path(cafe).string()}, "a non-ASCII argument"); + if (!wargv()[1] || cafe != wargv()[1]) { + std::fprintf(stderr, "FAIL: __wargv[1] is not L\"caf\\u00e9\"\n"); + ++failures; + } + + if (failures) return 1; + std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact\n"); + return 0; +} -- cgit v1.2.3