From fa4d54f22d8f20a4fd9ef9fc1880e36bbe0a3957 Mon Sep 17 00:00:00 2001 From: Emre Demir <274893088+EmreO33@users.noreply.github.com> Date: Sun, 22 Nov 2026 13:28:05 +0300 Subject: process shim: keep the game's command line wide so a non-ASCII argument can't crash --run (RT-84) main.cpp turned each game argument into std::filesystem::path::string(), which throws std::system_error under MSVC when the ANSI code page has no mapping for a character. Nothing caught it, so gta6rt --run ended in 0xc0000409 before the game started. set_command_line now takes wide strings: __wargv and GetCommandLineW hold the arguments exactly, and the narrow argv and GetCommandLineA are their CP_ACP text from WideCharToMultiByte, which replaces a character the code page lacks instead of failing, as the CRT does. process_cmdline_test passes wide arguments and checks a Latin-1 one and a Greek, Cyrillic and CJK one that no single-byte code page holds, so the crash is covered on every system locale, not only 1253. --- runtime/test/process_cmdline_test.cpp | 52 +++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 17 deletions(-) (limited to 'runtime/test/process_cmdline_test.cpp') diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp index 0df7b4e86..4c78835bb 100644 --- a/runtime/test/process_cmdline_test.cpp +++ b/runtime/test/process_cmdline_test.cpp @@ -1,11 +1,10 @@ // 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. +// main.cpp passed, whatever the system code page (RT-84). No game files. #include #include #include -#include #include #include @@ -20,8 +19,13 @@ wchar_t** wargv() { 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); +char** argv() { + using Fn = char*** (__cdecl*)(); + return *reinterpret_cast(rt::shims::find("ucrtbase.dll", "__p___argv"))(); +} + +void round_trip(const std::vector& args, const char* what) { + rt::process::set_command_line(L"game.exe", args); using Fn = LPWSTR(WINAPI*)(); const LPWSTR line = reinterpret_cast(rt::shims::find("kernel32.dll", "GetCommandLineW"))(); int count = 0; @@ -34,23 +38,37 @@ void round_trip(const std::vector& args, const char* what) { } 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"); +// __wargv[1] must be `want` exactly, and the narrow argv[1] its ANSI code page text (the CRT's replacement for a +// character the code page lacks), not a crash. +void check_non_ascii(const std::wstring& want, const char* what) { + round_trip({want}, what); + if (!wargv()[1] || want != wargv()[1]) { + std::fprintf(stderr, "FAIL %s: __wargv[1] is not the argument passed\n", what); + ++failures; + } + char expected[64] = {}; + WideCharToMultiByte(CP_ACP, 0, want.c_str(), -1, expected, sizeof(expected), nullptr, nullptr); + if (!argv()[1] || std::string(expected) != argv()[1]) { + std::fprintf(stderr, "FAIL %s: argv[1] is not the code page %u text of the argument\n", what, GetACP()); ++failures; } +} +} // namespace + +int main() { + round_trip({L"plain", L"with space"}, "plain and spaced arguments"); + round_trip({L"C:\\My Games\\", L"next"}, "a quoted argument ending in a backslash"); + round_trip({L"a\\\"b"}, "a backslash before a quote"); + round_trip({L"\\\\server\\share\\x y"}, "a UNC path with a space"); + + // In code page 1252 but not 1253 (Greek), where path::string() used to throw (RT-84). + check_non_ascii(L"caf\u00e9", "a Latin-1 argument"); + // Greek, Cyrillic and CJK together: no single-byte code page holds all of them, so this covers every PC. + check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page"); if (failures) return 1; - std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact\n"); + std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact (code page %u)\n", + GetACP()); return 0; } -- cgit v1.2.3