Skip to content

Commit 922b2a0

Browse files
[3.7] bpo-31446: Copy command line that should be passed to CreateProcessW(). (GH-11141). (GH-11149)
(cherry picked from commit 7b36016) Co-authored-by: Vladimir Matveev <[email protected]>
1 parent f27f0d2 commit 922b2a0

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Copy command line that was passed to CreateProcessW since this function can
2+
change the content of the input buffer.

Modules/_winapi.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,8 @@ getattributelist(PyObject *obj, const char *name, AttributeList *attribute_list)
974974
_winapi.CreateProcess
975975
976976
application_name: Py_UNICODE(accept={str, NoneType})
977-
command_line: Py_UNICODE(accept={str, NoneType})
977+
command_line: object
978+
Can be str or None
978979
proc_attrs: object
979980
Ignored internally, can be None.
980981
thread_attrs: object
@@ -994,19 +995,20 @@ process ID, and thread ID.
994995

995996
static PyObject *
996997
_winapi_CreateProcess_impl(PyObject *module, Py_UNICODE *application_name,
997-
Py_UNICODE *command_line, PyObject *proc_attrs,
998+
PyObject *command_line, PyObject *proc_attrs,
998999
PyObject *thread_attrs, BOOL inherit_handles,
9991000
DWORD creation_flags, PyObject *env_mapping,
10001001
Py_UNICODE *current_directory,
10011002
PyObject *startup_info)
1002-
/*[clinic end generated code: output=4652a33aff4b0ae1 input=4a43b05038d639bb]*/
1003+
/*[clinic end generated code: output=2ecaab46a05e3123 input=42ac293eaea03fc4]*/
10031004
{
10041005
PyObject *ret = NULL;
10051006
BOOL result;
10061007
PROCESS_INFORMATION pi;
10071008
STARTUPINFOEXW si;
10081009
PyObject *environment = NULL;
10091010
wchar_t *wenvironment;
1011+
wchar_t *command_line_copy = NULL;
10101012
AttributeList attribute_list = {0};
10111013

10121014
ZeroMemory(&si, sizeof(si));
@@ -1041,10 +1043,23 @@ _winapi_CreateProcess_impl(PyObject *module, Py_UNICODE *application_name,
10411043
goto cleanup;
10421044

10431045
si.lpAttributeList = attribute_list.attribute_list;
1046+
if (PyUnicode_Check(command_line)) {
1047+
command_line_copy = PyUnicode_AsWideCharString(command_line, NULL);
1048+
if (command_line_copy == NULL) {
1049+
goto cleanup;
1050+
}
1051+
}
1052+
else if (command_line != Py_None) {
1053+
PyErr_Format(PyExc_TypeError,
1054+
"CreateProcess() argument 2 must be str or None, not %s",
1055+
Py_TYPE(command_line)->tp_name);
1056+
goto cleanup;
1057+
}
1058+
10441059

10451060
Py_BEGIN_ALLOW_THREADS
10461061
result = CreateProcessW(application_name,
1047-
command_line,
1062+
command_line_copy,
10481063
NULL,
10491064
NULL,
10501065
inherit_handles,
@@ -1068,6 +1083,7 @@ _winapi_CreateProcess_impl(PyObject *module, Py_UNICODE *application_name,
10681083
pi.dwThreadId);
10691084

10701085
cleanup:
1086+
PyMem_Free(command_line_copy);
10711087
Py_XDECREF(environment);
10721088
freeattributelist(&attribute_list);
10731089

Modules/clinic/_winapi.c.h

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)