[PATCH v5 0/2] MR9882: kernelbase: Trim whitespaces from the quoted 'cmdline'
If 'name' does not have a suffix, a suffix will be appended in find_exe_file. If 'name' contains spaces, it will result in an incorrect filename being concatenated. In the log of a certain installer, I saw an entry like this: trace:process:CreateProcessInternalW app (null) cmdline L"\"C:\\Program Files\\some folder\\7za \" x -y -aos \"-oC://Program Files//target folder/\" \"C://Program Files//source.zip\"" Signed-off-by: YeshunYe <yeyeshun@uniontech.com> -- v5: kernelbase: Trim whitespaces from the quoted 'cmdline' https://gitlab.winehq.org/wine/wine/-/merge_requests/9882
From: YeshunYe <yeyeshun@uniontech.com> test for cmd with quotes and whitespaces Signed-off-by: YeshunYe <yeyeshun@uniontech.com> --- dlls/kernel32/tests/process.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/dlls/kernel32/tests/process.c b/dlls/kernel32/tests/process.c index 9d6dfb126e2..a77e2f4f328 100644 --- a/dlls/kernel32/tests/process.c +++ b/dlls/kernel32/tests/process.c @@ -1155,6 +1155,41 @@ static void test_CommandLine(void) cmdline = GetCommandLineW(); ok(cmdline == cmdline_backup, "Expected cached address from TEB, got %p\n", cmdline); NtCurrentTeb()->Peb->ProcessParameters->CommandLine.Buffer = cmdline_backup; + + /* Test quoted command line without file extension*/ + sprintf(buffer, "%s", selfname); + p = strrchr(buffer, '.'); + *p = 0; + + get_file_name(resfile); + sprintf(buffer2, "\" %s\" process dump \"%s\"", buffer, resfile); + ret = CreateProcessA(NULL, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info); + ok(!ret, "CreateProcessA unexpectedly succeeded\n"); + ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError()); + + get_file_name(resfile); + sprintf(buffer2, "\"%s \" process dump \"%s\"", buffer, resfile); + ret = CreateProcessA(NULL, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info); + todo_wine + ok(ret, "CreateProcess (%s) failed : %ld\n", buffer, GetLastError()); + if (info.hProcess) + { + wait_child_process(&info); + release_memory(); + DeleteFileA(resfile); + } + + get_file_name(resfile); + sprintf(buffer2, "\"\t%s\" process dump \"%s\"", buffer, resfile); + ret = CreateProcessA(NULL, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info); + ok(!ret, "CreateProcessA unexpectedly succeeded\n"); + ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError()); + + get_file_name(resfile); + sprintf(buffer2, "\"%s\t\" process dump \"%s\"", buffer, resfile); + ret = CreateProcessA(NULL, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info); + ok(!ret, "CreateProcessA unexpectedly succeeded\n"); + ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError()); } static void test_Directory(void) -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/9882
From: YeshunYe <yeyeshun@uniontech.com> If 'name' does not have a suffix, a suffix will be appended in find_exe_file. If 'name' contains spaces, it will result in an incorrect filename being concatenated. Signed-off-by: YeshunYe <yeyeshun@uniontech.com> --- dlls/kernel32/tests/process.c | 1 - dlls/kernelbase/process.c | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/dlls/kernel32/tests/process.c b/dlls/kernel32/tests/process.c index a77e2f4f328..61c05e6ff0a 100644 --- a/dlls/kernel32/tests/process.c +++ b/dlls/kernel32/tests/process.c @@ -1170,7 +1170,6 @@ static void test_CommandLine(void) get_file_name(resfile); sprintf(buffer2, "\"%s \" process dump \"%s\"", buffer, resfile); ret = CreateProcessA(NULL, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info); - todo_wine ok(ret, "CreateProcess (%s) failed : %ld\n", buffer, GetLastError()); if (info.hProcess) { diff --git a/dlls/kernelbase/process.c b/dlls/kernelbase/process.c index 3656e40280d..b31aa08a39b 100644 --- a/dlls/kernelbase/process.c +++ b/dlls/kernelbase/process.c @@ -88,6 +88,8 @@ static WCHAR *get_file_name( WCHAR *cmdline, WCHAR *buffer, DWORD buflen ) if (cmdline[0] == '"' && (p = wcschr( cmdline + 1, '"' ))) { int len = p - cmdline - 1; + /* trim spaces in quotes */ + while (len && cmdline[len] == L' ') len--; /* extract the quoted portion as file name */ if (!(name = RtlAllocateHeap( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return NULL; memcpy( name, cmdline + 1, len * sizeof(WCHAR) ); -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/9882
On Mon Jan 19 00:49:44 2026 +0000, Yeshun Ye wrote:
changed this line in [version 5 of the diff](/wine/wine/-/merge_requests/9882/diffs?diff_id=238613&start_sha=9070b04acdf354cede498427b01e5488e855959d#361f2e2f32e8486d0267db3f58ea3b45e8904666_95_92) Your approach is indeed more elegant. I initially handled the case where the quotes in the `cmdline` contained only spaces. Later, through comparative testing, I found that a `cmdline` starting with spaces would cause a failure in Windows. This indicates that Windows does not trim leading spaces, so I removed the processing for `start`. However, I didn't re-optimize the code afterward.
-- https://gitlab.winehq.org/wine/wine/-/merge_requests/9882#note_127290
This merge request was approved by eric pouech. -- https://gitlab.winehq.org/wine/wine/-/merge_requests/9882
participants (3)
-
eric pouech (@epo) -
Yeshun Ye (@yeyeshun) -
YeshunYe