From 16c332fabb49c0ced73f034178f435a71fefa814 Mon Sep 17 00:00:00 2001 From: Zachary Ware Date: Fri, 30 Mar 2018 15:50:50 -0500 Subject: [PATCH 1/3] bpo-33191: Fix refleak in posix_spawn --- Modules/posixmodule.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index e6721032f211b7d..83642bbacfad868 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -5142,6 +5142,7 @@ os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, Py_ssize_t argc, envc; PyObject* result = NULL; PyObject* seq = NULL; + PyObject* file_actions_obj = NULL; /* posix_spawn has three arguments: (path, argv, env), where @@ -5194,17 +5195,17 @@ os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, if(seq == NULL) { goto exit; } - PyObject* file_actions_obj; PyObject* mode_obj; for (int i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) { file_actions_obj = PySequence_Fast_GET_ITEM(seq, i); - - if(!PySequence_Check(file_actions_obj) | !PySequence_Size(file_actions_obj)) { - PyErr_SetString(PyExc_TypeError,"Each file_action element must be a non empty sequence"); + if (file_actions_obj == NULL) { + goto exit; + } + file_actions_obj = PySequence_Fast(file_actions_obj, "Each file_action element must be a non-empty sequence"); + if (file_actions_obj == NULL) { goto exit; } - mode_obj = PySequence_Fast_GET_ITEM(file_actions_obj, 0); int mode = PyLong_AsLong(mode_obj); @@ -5219,19 +5220,19 @@ os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, goto exit; } - long open_fd = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 1)); + long open_fd = PyLong_AsLong(PySequence_Fast_GET_ITEM(file_actions_obj, 1)); if(PyErr_Occurred()) { goto exit; } - const char* open_path = PyUnicode_AsUTF8(PySequence_GetItem(file_actions_obj, 2)); + const char* open_path = PyUnicode_AsUTF8(PySequence_Fast_GET_ITEM(file_actions_obj, 2)); if(open_path == NULL) { goto exit; } - long open_oflag = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 3)); + long open_oflag = PyLong_AsLong(PySequence_Fast_GET_ITEM(file_actions_obj, 3)); if(PyErr_Occurred()) { goto exit; } - long open_mode = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 4)); + long open_mode = PyLong_AsLong(PySequence_Fast_GET_ITEM(file_actions_obj, 4)); if(PyErr_Occurred()) { goto exit; } @@ -5248,7 +5249,7 @@ os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, goto exit; } - long close_fd = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 1)); + long close_fd = PyLong_AsLong(PySequence_Fast_GET_ITEM(file_actions_obj, 1)); if(PyErr_Occurred()) { goto exit; } @@ -5264,11 +5265,11 @@ os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, goto exit; } - long fd1 = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 1)); + long fd1 = PyLong_AsLong(PySequence_Fast_GET_ITEM(file_actions_obj, 1)); if(PyErr_Occurred()) { goto exit; } - long fd2 = PyLong_AsLong(PySequence_GetItem(file_actions_obj, 2)); + long fd2 = PyLong_AsLong(PySequence_Fast_GET_ITEM(file_actions_obj, 2)); if(PyErr_Occurred()) { goto exit; } @@ -5282,7 +5283,9 @@ os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, PyErr_SetString(PyExc_TypeError,"Unknown file_actions identifier"); goto exit; } + Py_DECREF(file_actions_obj); } + file_actions_obj = NULL; } _Py_BEGIN_SUPPRESS_IPH @@ -5297,6 +5300,7 @@ os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, exit: Py_XDECREF(seq); + Py_XDECREF(file_actions_obj); if(file_actionsp) { posix_spawn_file_actions_destroy(file_actionsp); From 452479b23d03723397338381e6ff7cbb13b3e41e Mon Sep 17 00:00:00 2001 From: Zachary Ware Date: Fri, 30 Mar 2018 16:13:10 -0500 Subject: [PATCH 2/3] Add blurb --- .../Core and Builtins/2018-03-30-16-13-01.bpo-33191.P143W_.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-03-30-16-13-01.bpo-33191.P143W_.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-03-30-16-13-01.bpo-33191.P143W_.rst b/Misc/NEWS.d/next/Core and Builtins/2018-03-30-16-13-01.bpo-33191.P143W_.rst new file mode 100644 index 000000000000000..dc742b9de30389e --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-03-30-16-13-01.bpo-33191.P143W_.rst @@ -0,0 +1 @@ +Fixed a refleak in posix_spawn. From 775649fabf50ef770e430f63b7cbdf6bdd695ac2 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Fri, 30 Mar 2018 15:52:15 -0700 Subject: [PATCH 3/3] mention the os module --- .../Core and Builtins/2018-03-30-16-13-01.bpo-33191.P143W_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-03-30-16-13-01.bpo-33191.P143W_.rst b/Misc/NEWS.d/next/Core and Builtins/2018-03-30-16-13-01.bpo-33191.P143W_.rst index dc742b9de30389e..ddfe3d9b9aff0f4 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2018-03-30-16-13-01.bpo-33191.P143W_.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2018-03-30-16-13-01.bpo-33191.P143W_.rst @@ -1 +1 @@ -Fixed a refleak in posix_spawn. +Fixed a refleak in os.posix_spawn.