diff options
| author | Emre Demir <274893088+EmreO33@users.noreply.github.com> | 2026-11-22 13:28:05 +0300 |
|---|---|---|
| committer | Emre Demir <274893088+EmreO33@users.noreply.github.com> | 2026-11-22 13:28:05 +0300 |
| commit | fa4d54f22d8f20a4fd9ef9fc1880e36bbe0a3957 (patch) | |
| tree | 0af904832274bfa68e4d17ee2592a29010db34ae /runtime/test/process_cmdline_test.cpp | |
| parent | f85582e23d4ab9e19acbf694358f137d6b8531b1 (diff) | |
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.
Diffstat (limited to 'runtime/test/process_cmdline_test.cpp')
| -rw-r--r-- | runtime/test/process_cmdline_test.cpp | 52 |
1 files changed, 35 insertions, 17 deletions
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 @@ | |||
| 1 | // The process shim's command line round-trips: what GetCommandLineW says must parse with CommandLineToArgvW (the | 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 | 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. | 3 | // main.cpp passed, whatever the system code page (RT-84). No game files. |
| 4 | #include <windows.h> | 4 | #include <windows.h> |
| 5 | #include <shellapi.h> | 5 | #include <shellapi.h> |
| 6 | 6 | ||
| 7 | #include <cstdio> | 7 | #include <cstdio> |
| 8 | #include <filesystem> | ||
| 9 | #include <string> | 8 | #include <string> |
| 10 | #include <vector> | 9 | #include <vector> |
| 11 | 10 | ||
| @@ -20,8 +19,13 @@ wchar_t** wargv() { | |||
| 20 | return *reinterpret_cast<Fn>(rt::shims::find("ucrtbase.dll", "__p___wargv"))(); | 19 | return *reinterpret_cast<Fn>(rt::shims::find("ucrtbase.dll", "__p___wargv"))(); |
| 21 | } | 20 | } |
| 22 | 21 | ||
| 23 | void round_trip(const std::vector<std::string>& args, const char* what) { | 22 | char** argv() { |
| 24 | rt::process::set_command_line("game.exe", args); | 23 | using Fn = char*** (__cdecl*)(); |
| 24 | return *reinterpret_cast<Fn>(rt::shims::find("ucrtbase.dll", "__p___argv"))(); | ||
| 25 | } | ||
| 26 | |||
| 27 | void round_trip(const std::vector<std::wstring>& args, const char* what) { | ||
| 28 | rt::process::set_command_line(L"game.exe", args); | ||
| 25 | using Fn = LPWSTR(WINAPI*)(); | 29 | using Fn = LPWSTR(WINAPI*)(); |
| 26 | const LPWSTR line = reinterpret_cast<Fn>(rt::shims::find("kernel32.dll", "GetCommandLineW"))(); | 30 | const LPWSTR line = reinterpret_cast<Fn>(rt::shims::find("kernel32.dll", "GetCommandLineW"))(); |
| 27 | int count = 0; | 31 | int count = 0; |
| @@ -34,23 +38,37 @@ void round_trip(const std::vector<std::string>& args, const char* what) { | |||
| 34 | } | 38 | } |
| 35 | LocalFree(parsed); | 39 | LocalFree(parsed); |
| 36 | } | 40 | } |
| 37 | } // namespace | ||
| 38 | 41 | ||
| 39 | int main() { | 42 | // __wargv[1] must be `want` exactly, and the narrow argv[1] its ANSI code page text (the CRT's replacement for a |
| 40 | round_trip({"plain", "with space"}, "plain and spaced arguments"); | 43 | // character the code page lacks), not a crash. |
| 41 | round_trip({"C:\\My Games\\", "next"}, "a quoted argument ending in a backslash"); | 44 | void check_non_ascii(const std::wstring& want, const char* what) { |
| 42 | round_trip({"a\\\"b"}, "a backslash before a quote"); | 45 | round_trip({want}, what); |
| 43 | round_trip({"\\\\server\\share\\x y"}, "a UNC path with a space"); | 46 | if (!wargv()[1] || want != wargv()[1]) { |
| 44 | 47 | std::fprintf(stderr, "FAIL %s: __wargv[1] is not the argument passed\n", what); | |
| 45 | // main.cpp passes game arguments as std::filesystem::path::string(): ANSI code page text, not UTF-8. | 48 | ++failures; |
| 46 | const std::wstring cafe = L"caf\u00e9"; | 49 | } |
| 47 | round_trip({std::filesystem::path(cafe).string()}, "a non-ASCII argument"); | 50 | char expected[64] = {}; |
| 48 | if (!wargv()[1] || cafe != wargv()[1]) { | 51 | WideCharToMultiByte(CP_ACP, 0, want.c_str(), -1, expected, sizeof(expected), nullptr, nullptr); |
| 49 | std::fprintf(stderr, "FAIL: __wargv[1] is not L\"caf\\u00e9\"\n"); | 52 | if (!argv()[1] || std::string(expected) != argv()[1]) { |
| 53 | std::fprintf(stderr, "FAIL %s: argv[1] is not the code page %u text of the argument\n", what, GetACP()); | ||
| 50 | ++failures; | 54 | ++failures; |
| 51 | } | 55 | } |
| 56 | } | ||
| 57 | } // namespace | ||
| 58 | |||
| 59 | int main() { | ||
| 60 | round_trip({L"plain", L"with space"}, "plain and spaced arguments"); | ||
| 61 | round_trip({L"C:\\My Games\\", L"next"}, "a quoted argument ending in a backslash"); | ||
| 62 | round_trip({L"a\\\"b"}, "a backslash before a quote"); | ||
| 63 | round_trip({L"\\\\server\\share\\x y"}, "a UNC path with a space"); | ||
| 64 | |||
| 65 | // In code page 1252 but not 1253 (Greek), where path::string() used to throw (RT-84). | ||
| 66 | check_non_ascii(L"caf\u00e9", "a Latin-1 argument"); | ||
| 67 | // Greek, Cyrillic and CJK together: no single-byte code page holds all of them, so this covers every PC. | ||
| 68 | check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page"); | ||
| 52 | 69 | ||
| 53 | if (failures) return 1; | 70 | if (failures) return 1; |
| 54 | std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact\n"); | 71 | std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact (code page %u)\n", |
| 72 | GetACP()); | ||
| 55 | return 0; | 73 | return 0; |
| 56 | } | 74 | } |