From 7423214d8deebbbcdab66090e185129decfaa799 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Thu, 1 May 2025 22:15:19 +0100 Subject: [PATCH] subprocess: Do not escape double quotes for command line arguments on Windows * refactor: Guard `util::quote_argument()` with `#ifdef __USING_WINDOWS__` The `util::quote_argument()` function is specific to Windows and is used in code already guarded by `#ifdef __USING_WINDOWS__`. * Do not escape double quotes for command line arguments on Windows This change fixes the handling of double quotes and aligns the behavior with Python's `Popen` class. For example: ``` >py -3 >>> import subprocess >>> p = subprocess.Popen("cmd.exe /c dir \"C:\\Program Files\"", stdout=subprocess.PIPE, text=True) >>> print(f"Captured stdout:\n{stdout}") ``` Currently, the same command line processed by the `quote_argument()` function looks like `cmd.exe /c dir "\"C:\Program" "Files\""`, which is broken. With this change, it looks correct: `cmd.exe /c dir "C:\Program Files"`. Github-Pull: arun11299/cpp-subprocess#113 Rebased-From: ed313971c04ac10dc006104aba07d016ffc6542a --- src/util/subprocess.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/subprocess.h b/src/util/subprocess.h index 91f84a290bd..6fa84af2af7 100644 --- a/src/util/subprocess.h +++ b/src/util/subprocess.h @@ -171,6 +171,7 @@ public: //-------------------------------------------------------------------- namespace util { +#ifdef __USING_WINDOWS__ inline void quote_argument(const std::wstring &argument, std::wstring &command_line, bool force) { @@ -181,7 +182,7 @@ namespace util // if (force == false && argument.empty() == false && - argument.find_first_of(L" \t\n\v\"") == argument.npos) { + argument.find_first_of(L" \t\n\v") == argument.npos) { command_line.append(argument); } else { @@ -231,7 +232,6 @@ namespace util } } -#ifdef __USING_WINDOWS__ inline std::string get_last_error(DWORD errorMessageID) { if (errorMessageID == 0)