Don't pass read-only buffer to CreateProcess as lpCommandLine
[TortoiseGit.git] / src / Utils / CreateProcessHelper.h
blob152531c95dd75ba296f6f3f51a341f712b54160a
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2016 - TortoiseGit
4 // Copyright (C) 2009-2010 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
22 /**
23 * A helper class for invoking CreateProcess(). The lpProcessInformation
24 * can point to an uninitialized struct - it's memset to all zeroes inside.
27 class CCreateProcessHelper
29 public:
30 static bool CreateProcess(LPCTSTR lpApplicationName,
31 LPTSTR lpCommandLine,
32 LPCTSTR lpCurrentDirectory,
33 LPPROCESS_INFORMATION lpProcessInformation);
34 static bool CreateProcess(LPCTSTR lpApplicationName,
35 LPTSTR lpCommandLine,
36 LPPROCESS_INFORMATION lpProcessInformation);
38 static bool CreateProcessDetached(LPCTSTR lpApplicationName,
39 LPTSTR lpCommandLine,
40 LPCTSTR lpCurrentDirectory);
41 static bool CreateProcessDetached(LPCTSTR lpApplicationName,
42 LPTSTR lpCommandLine);
43 static bool CreateProcessDetached(LPCTSTR lpApplicationName, LPCTSTR lpCommandLine);
46 inline bool CCreateProcessHelper::CreateProcess(LPCTSTR applicationName,
47 LPTSTR commandLine, LPCTSTR currentDirectory,
48 LPPROCESS_INFORMATION processInfo)
50 STARTUPINFO startupInfo = { 0 };
51 startupInfo.cb = sizeof(STARTUPINFO);
53 memset(processInfo, 0, sizeof(PROCESS_INFORMATION));
54 const BOOL result = ::CreateProcess(applicationName, commandLine, nullptr, nullptr, FALSE, CREATE_UNICODE_ENVIRONMENT, nullptr, currentDirectory, &startupInfo, processInfo);
55 return result != 0;
58 inline bool CCreateProcessHelper::CreateProcess(LPCTSTR applicationName,
59 LPTSTR commandLine, LPPROCESS_INFORMATION processInformation)
61 return CreateProcess( applicationName, commandLine, 0, processInformation );
64 inline bool CCreateProcessHelper::CreateProcessDetached(LPCTSTR lpApplicationName,
65 LPTSTR lpCommandLine, LPCTSTR lpCurrentDirectory)
67 PROCESS_INFORMATION process;
68 if (!CreateProcess(lpApplicationName, lpCommandLine, lpCurrentDirectory, &process))
69 return false;
71 CloseHandle(process.hThread);
72 CloseHandle(process.hProcess);
73 return true;
76 inline bool CCreateProcessHelper::CreateProcessDetached(LPCTSTR lpApplicationName,
77 LPTSTR lpCommandLine)
79 return CreateProcessDetached(lpApplicationName, lpCommandLine, 0);
82 inline bool CCreateProcessHelper::CreateProcessDetached(LPCTSTR lpApplicationName, LPCTSTR lpCommandLine)
84 // CreateProcess may modify buffer specified by lpCommandLine:
85 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425
86 // [[
87 // The Unicode version of this function, CreateProcessW, can modify
88 // the contents of this string. Therefore, this parameter cannot be a
89 // pointer to read - only memory(such as a const variable or a literal
90 // string).If this parameter is a constant string, the function may
91 // cause an access violation.
92 // ]]
93 LPWSTR commandLineBuf = nullptr;
94 if (lpCommandLine)
95 commandLineBuf = _tcsdup(lpCommandLine);
97 return CreateProcessDetached(lpApplicationName, commandLineBuf, 0);