Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b36016

Browse files
vladimaserhiy-storchaka
authored andcommitted
bpo-31446: Copy command line that should be passed to CreateProcessW(). (GH-11141)
1 parent 08c2ba0 commit 7b36016

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

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
@@ -975,7 +975,8 @@ getattributelist(PyObject *obj, const char *name, AttributeList *attribute_list)
975975
_winapi.CreateProcess
976976
977977
application_name: Py_UNICODE(accept={str, NoneType})
978-
command_line: Py_UNICODE(accept={str, NoneType})
978+
command_line: object
979+
Can be str or None
979980
proc_attrs: object
980981
Ignored internally, can be None.
981982
thread_attrs: object
@@ -995,19 +996,20 @@ process ID, and thread ID.
995996

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

10131015
ZeroMemory(&si, sizeof(si));
@@ -1042,10 +1044,23 @@ _winapi_CreateProcess_impl(PyObject *module, Py_UNICODE *application_name,
10421044
goto cleanup;
10431045

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

10461061
Py_BEGIN_ALLOW_THREADS
10471062
result = CreateProcessW(application_name,
1048-
command_line,
1063+
command_line_copy,
10491064
NULL,
10501065
NULL,
10511066
inherit_handles,
@@ -1069,6 +1084,7 @@ _winapi_CreateProcess_impl(PyObject *module, Py_UNICODE *application_name,
10691084
pi.dwThreadId);
10701085

10711086
cleanup:
1087+
PyMem_Free(command_line_copy);
10721088
Py_XDECREF(environment);
10731089
freeattributelist(&attribute_list);
10741090

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)