From 1173b14c439b7c7b0af5caca25956090cbf3e329 Mon Sep 17 00:00:00 2001 From: stasoid Date: Mon, 23 Dec 2024 18:25:45 +0500 Subject: [PATCH] LibCore: Pass correct number of arguments to the Process on Windows Incorrect behavior of CreateProcess: CreateProcess("a.exe", "b c", ...) -> a.exe::main::argv == ["b", "c"] (wrong) CreateProcess(0, "a.exe b c", ...) -> a.exe::main::argv == ["a.exe", "b", "c"] (right) https://learn.microsoft.com/en-us/cpp/cpp/main-function-command-line-args "If you use both the first and second arguments (lpApplicationName and lpCommandLine), argv[0] may not be the executable name." This means first argument of CreateProcess should never be used. ----------------------------------------------------------------------- Searching for executable in path is suppressed now by prepending "./" Additional bonus of the new code: .exe extension does not need to be specified. --- Libraries/LibCore/ProcessWindows.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Libraries/LibCore/ProcessWindows.cpp b/Libraries/LibCore/ProcessWindows.cpp index 59cdeb80444..784553ed0ba 100644 --- a/Libraries/LibCore/ProcessWindows.cpp +++ b/Libraries/LibCore/ProcessWindows.cpp @@ -42,12 +42,11 @@ ErrorOr Process::spawn(ProcessSpawnOptions const& options) // file actions are not supported VERIFY(options.file_actions.is_empty()); - char const* program_path = 0; StringBuilder builder; - if (options.search_for_executable_in_path) - builder.appendff("\"{}\" ", options.executable); + if (!options.search_for_executable_in_path && !options.executable.find_any_of("\\/:"sv).has_value()) + builder.appendff("\"./{}\" ", options.executable); else - program_path = options.executable.characters(); + builder.appendff("\"{}\" ", options.executable); builder.join(' ', options.arguments); builder.append('\0'); @@ -59,7 +58,7 @@ ErrorOr Process::spawn(ProcessSpawnOptions const& options) PROCESS_INFORMATION process_info = {}; BOOL result = CreateProcess( - program_path, + NULL, (char*)command_line.data(), NULL, // process security attributes NULL, // primary thread security attributes