summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na@python.org>2021-10-07 02:10:15 (GMT)
committerGitHub <noreply@github.com>2021-10-07 02:10:15 (GMT)
commit660718dba57624720c2a9832f10f29ace59c77cf (patch)
tree22b647b55c646efcd6b3739e30dcc464372c041a /Objects
parent4f161e65a011f287227c944fad9987446644041f (diff)
downloadcpython-660718dba57624720c2a9832f10f29ace59c77cf.zip
cpython-660718dba57624720c2a9832f10f29ace59c77cf.tar.gz
cpython-660718dba57624720c2a9832f10f29ace59c77cf.tar.bz2
[3.9] bpo-45385: Fix reference leak from descr_check (GH-28719) (GH-28780)
(cherry picked from commit e6ff4eba6da9b64aed235ba8d730b5645f71955c) Co-authored-by: Dong-hee Na <donghee.na@python.org>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/descrobject.c80
1 files changed, 41 insertions, 39 deletions
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index fce9cdd..075a92d 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -72,13 +72,8 @@ wrapperdescr_repr(PyWrapperDescrObject *descr)
}
static int
-descr_check(PyDescrObject *descr, PyObject *obj, PyObject **pres)
+descr_check(PyDescrObject *descr, PyObject *obj)
{
- if (obj == NULL) {
- Py_INCREF(descr);
- *pres = (PyObject *)descr;
- return 1;
- }
if (!PyObject_TypeCheck(obj, descr->d_type)) {
PyErr_Format(PyExc_TypeError,
"descriptor '%V' for '%.100s' objects "
@@ -86,8 +81,7 @@ descr_check(PyDescrObject *descr, PyObject *obj, PyObject **pres)
descr_name((PyDescrObject *)descr), "?",
descr->d_type->tp_name,
Py_TYPE(obj)->tp_name);
- *pres = NULL;
- return 1;
+ return -1;
}
return 0;
}
@@ -137,10 +131,13 @@ classmethod_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
static PyObject *
method_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
{
- PyObject *res;
-
- if (descr_check((PyDescrObject *)descr, obj, &res))
- return res;
+ if (obj == NULL) {
+ Py_INCREF(descr);
+ return (PyObject *)descr;
+ }
+ if (descr_check((PyDescrObject *)descr, obj) < 0) {
+ return NULL;
+ }
if (descr->d_method->ml_flags & METH_METHOD) {
if (PyType_Check(type)) {
return PyCMethod_New(descr->d_method, obj, NULL, descr->d_common.d_type);
@@ -159,10 +156,13 @@ method_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
static PyObject *
member_get(PyMemberDescrObject *descr, PyObject *obj, PyObject *type)
{
- PyObject *res;
-
- if (descr_check((PyDescrObject *)descr, obj, &res))
- return res;
+ if (obj == NULL) {
+ Py_INCREF(descr);
+ return (PyObject *)descr;
+ }
+ if (descr_check((PyDescrObject *)descr, obj) < 0) {
+ return NULL;
+ }
if (descr->d_member->flags & READ_RESTRICTED) {
if (PySys_Audit("object.__getattr__", "Os",
@@ -177,10 +177,13 @@ member_get(PyMemberDescrObject *descr, PyObject *obj, PyObject *type)
static PyObject *
getset_get(PyGetSetDescrObject *descr, PyObject *obj, PyObject *type)
{
- PyObject *res;
-
- if (descr_check((PyDescrObject *)descr, obj, &res))
- return res;
+ if (obj == NULL) {
+ Py_INCREF(descr);
+ return (PyObject *)descr;
+ }
+ if (descr_check((PyDescrObject *)descr, obj) < 0) {
+ return NULL;
+ }
if (descr->d_getset->get != NULL)
return descr->d_getset->get(obj, descr->d_getset->closure);
PyErr_Format(PyExc_AttributeError,
@@ -193,16 +196,18 @@ getset_get(PyGetSetDescrObject *descr, PyObject *obj, PyObject *type)
static PyObject *
wrapperdescr_get(PyWrapperDescrObject *descr, PyObject *obj, PyObject *type)
{
- PyObject *res;
-
- if (descr_check((PyDescrObject *)descr, obj, &res))
- return res;
+ if (obj == NULL) {
+ Py_INCREF(descr);
+ return (PyObject *)descr;
+ }
+ if (descr_check((PyDescrObject *)descr, obj) < 0) {
+ return NULL;
+ }
return PyWrapper_New((PyObject *)descr, obj);
}
static int
-descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
- int *pres)
+descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value)
{
assert(obj != NULL);
if (!PyObject_TypeCheck(obj, descr->d_type)) {
@@ -212,8 +217,7 @@ descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
descr_name(descr), "?",
descr->d_type->tp_name,
Py_TYPE(obj)->tp_name);
- *pres = -1;
- return 1;
+ return -1;
}
return 0;
}
@@ -221,23 +225,22 @@ descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
static int
member_set(PyMemberDescrObject *descr, PyObject *obj, PyObject *value)
{
- int res;
-
- if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
- return res;
+ if (descr_setcheck((PyDescrObject *)descr, obj, value) < 0) {
+ return -1;
+ }
return PyMember_SetOne((char *)obj, descr->d_member, value);
}
static int
getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value)
{
- int res;
-
- if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
- return res;
- if (descr->d_getset->set != NULL)
+ if (descr_setcheck((PyDescrObject *)descr, obj, value) < 0) {
+ return -1;
+ }
+ if (descr->d_getset->set != NULL) {
return descr->d_getset->set(obj, value,
descr->d_getset->closure);
+ }
PyErr_Format(PyExc_AttributeError,
"attribute '%V' of '%.100s' objects is not writable",
descr_name((PyDescrObject *)descr), "?",
@@ -264,8 +267,7 @@ method_check_args(PyObject *func, PyObject *const *args, Py_ssize_t nargs, PyObj
return -1;
}
PyObject *self = args[0];
- PyObject *dummy;
- if (descr_check((PyDescrObject *)func, self, &dummy)) {
+ if (descr_check((PyDescrObject *)func, self) < 0) {
return -1;
}
if (kwnames && PyTuple_GET_SIZE(kwnames)) {