bpo-46730: Fix refleak and tighten NULL checks (GH-31389)

``PyType_GetQualName`` returns a new reference.

Signed-off-by: Christian Heimes <christian@python.org>
This commit is contained in:
Christian Heimes 2022-02-17 21:27:42 +02:00 committed by GitHub
parent 98dd0aec2d
commit 9e06d03672
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1585,18 +1585,22 @@ property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
propertyobject *gs = (propertyobject *)self; propertyobject *gs = (propertyobject *)self;
if (gs->prop_get == NULL) { if (gs->prop_get == NULL) {
if (gs->prop_name != NULL) { PyObject *qualname = PyType_GetQualName(Py_TYPE(obj));
if (gs->prop_name != NULL && qualname != NULL) {
PyErr_Format(PyExc_AttributeError, PyErr_Format(PyExc_AttributeError,
"property %R of %R object has no getter", "property %R of %R object has no getter",
gs->prop_name, gs->prop_name,
PyType_GetQualName(Py_TYPE(obj))); qualname);
} }
else { else if (qualname != NULL) {
PyErr_Format(PyExc_AttributeError, PyErr_Format(PyExc_AttributeError,
"property of %R object has no getter", "property of %R object has no getter",
PyType_GetQualName(Py_TYPE(obj))); qualname);
} else {
PyErr_SetString(PyExc_AttributeError,
"property has no getter");
} }
Py_XDECREF(qualname);
return NULL; return NULL;
} }
@ -1617,20 +1621,24 @@ property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
} }
if (func == NULL) { if (func == NULL) {
if (gs->prop_name != NULL && obj != NULL) { PyObject *qualname = NULL;
if (obj != NULL) {
qualname = PyType_GetQualName(Py_TYPE(obj));
}
if (gs->prop_name != NULL && qualname != NULL) {
PyErr_Format(PyExc_AttributeError, PyErr_Format(PyExc_AttributeError,
value == NULL ? value == NULL ?
"property %R of %R object has no deleter" : "property %R of %R object has no deleter" :
"property %R of %R object has no setter", "property %R of %R object has no setter",
gs->prop_name, gs->prop_name,
PyType_GetQualName(Py_TYPE(obj))); qualname);
} }
else if (obj != NULL) { else if (qualname != NULL) {
PyErr_Format(PyExc_AttributeError, PyErr_Format(PyExc_AttributeError,
value == NULL ? value == NULL ?
"property of %R object has no deleter" : "property of %R object has no deleter" :
"property of %R object has no setter", "property of %R object has no setter",
PyType_GetQualName(Py_TYPE(obj))); qualname);
} }
else { else {
PyErr_SetString(PyExc_AttributeError, PyErr_SetString(PyExc_AttributeError,
@ -1638,6 +1646,7 @@ property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
"property has no deleter" : "property has no deleter" :
"property has no setter"); "property has no setter");
} }
Py_XDECREF(qualname);
return -1; return -1;
} }