From 2a05277001475d18a01a463873ed28ae738d5f45 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Thu, 28 Feb 2019 12:15:26 -0700 Subject: [PATCH 1/2] bpo-36150: Fix possible assertion failures due to _ctypes.c's PyCData_reduce() --- Modules/_ctypes/_ctypes.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 0d95d2b6f76eceb..da68608bf58d86a 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -2731,11 +2731,16 @@ PyCData_reduce(PyObject *myself, PyObject *args) "ctypes objects containing pointers cannot be pickled"); return NULL; } - return Py_BuildValue("O(O(NN))", - _unpickle, - Py_TYPE(myself), - PyObject_GetAttrString(myself, "__dict__"), - PyBytes_FromStringAndSize(self->b_ptr, self->b_size)); + PyObject *dict = PyObject_GetAttrString(myself, "__dict__"); + if (dict == NULL) { + return NULL; + } + PyObject *bytes = PyBytes_FromStringAndSize(self->b_ptr, self->b_size); + if (bytes == NULL) { + Py_DECREF(dict); + return NULL; + } + return Py_BuildValue("O(O(NN))", _unpickle, Py_TYPE(myself), dict, bytes); } static PyObject * From 1f533d5c33959a8c3bc119134c616725158fb5c8 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sun, 31 Mar 2019 08:53:59 -0600 Subject: [PATCH 2/2] Address the review comments. --- Modules/_ctypes/_ctypes.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index da68608bf58d86a..2a22895f40ee14d 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -2735,12 +2735,8 @@ PyCData_reduce(PyObject *myself, PyObject *args) if (dict == NULL) { return NULL; } - PyObject *bytes = PyBytes_FromStringAndSize(self->b_ptr, self->b_size); - if (bytes == NULL) { - Py_DECREF(dict); - return NULL; - } - return Py_BuildValue("O(O(NN))", _unpickle, Py_TYPE(myself), dict, bytes); + return Py_BuildValue("O(O(NN))", _unpickle, Py_TYPE(myself), dict, + PyBytes_FromStringAndSize(self->b_ptr, self->b_size)); } static PyObject *