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

Skip to content

Commit b2c7de4

Browse files
author
Michael W. Hudson
committed
Fix for
[ 784825 ] fix obscure crash in descriptor handling Should be applied to release23-maint and in all likelyhood release22-maint, too. Certainly doesn't apply to release21-maint.
1 parent f02bcee commit b2c7de4

3 files changed

Lines changed: 48 additions & 5 deletions

File tree

Lib/test/test_descr.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3938,6 +3938,36 @@ def __getattr__(self, attr):
39383938
except RuntimeError:
39393939
pass
39403940

3941+
def vicious_descriptor_nonsense():
3942+
# A potential segfault spotted by Thomas Wouters in mail to
3943+
# python-dev 2003-04-17, turned into an example & fixed by Michael
3944+
# Hudson just less than four months later...
3945+
if verbose:
3946+
print "Testing vicious_descriptor_nonsense..."
3947+
3948+
class Evil(object):
3949+
def __hash__(self):
3950+
return hash('attr')
3951+
def __eq__(self, other):
3952+
del C.attr
3953+
return 0
3954+
3955+
class Descr(object):
3956+
def __get__(self, ob, type=None):
3957+
return 1
3958+
3959+
class C(object):
3960+
attr = Descr()
3961+
3962+
c = C()
3963+
c.__dict__[Evil()] = 0
3964+
3965+
vereq(c.attr, 1)
3966+
# this makes a crash more likely:
3967+
import gc; gc.collect()
3968+
vereq(hasattr(c, 'attr'), False)
3969+
3970+
39413971
def test_main():
39423972
weakref_segfault() # Must be first, somehow
39433973
do_this_first()
@@ -4029,6 +4059,7 @@ def test_main():
40294059
proxysuper()
40304060
carloverre()
40314061
filefault()
4062+
vicious_descriptor_nonsense()
40324063

40334064
if verbose: print "All OK"
40344065

Objects/object.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,12 +1412,15 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
14121412
}
14131413
}
14141414

1415+
Py_XINCREF(descr);
1416+
14151417
f = NULL;
14161418
if (descr != NULL &&
14171419
PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
14181420
f = descr->ob_type->tp_descr_get;
14191421
if (f != NULL && PyDescr_IsData(descr)) {
14201422
res = f(descr, obj, (PyObject *)obj->ob_type);
1423+
Py_DECREF(descr);
14211424
goto done;
14221425
}
14231426
}
@@ -1445,19 +1448,21 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
14451448
res = PyDict_GetItem(dict, name);
14461449
if (res != NULL) {
14471450
Py_INCREF(res);
1451+
Py_XDECREF(descr);
14481452
goto done;
14491453
}
14501454
}
14511455
}
14521456

14531457
if (f != NULL) {
14541458
res = f(descr, obj, (PyObject *)obj->ob_type);
1459+
Py_DECREF(descr);
14551460
goto done;
14561461
}
14571462

14581463
if (descr != NULL) {
1459-
Py_INCREF(descr);
14601464
res = descr;
1465+
/* descr was already increfed above */
14611466
goto done;
14621467
}
14631468

Objects/typeobject.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,7 @@ type_getattro(PyTypeObject *type, PyObject *name)
20102010
return meta_get(meta_attribute, (PyObject *)type,
20112011
(PyObject *)metatype);
20122012
}
2013+
Py_INCREF(meta_attribute);
20132014
}
20142015

20152016
/* No data descriptor found on metatype. Look in tp_dict of this
@@ -2018,6 +2019,9 @@ type_getattro(PyTypeObject *type, PyObject *name)
20182019
if (attribute != NULL) {
20192020
/* Implement descriptor functionality, if any */
20202021
descrgetfunc local_get = attribute->ob_type->tp_descr_get;
2022+
2023+
Py_XDECREF(meta_attribute);
2024+
20212025
if (local_get != NULL) {
20222026
/* NULL 2nd argument indicates the descriptor was
20232027
* found on the target object itself (or a base) */
@@ -2031,13 +2035,16 @@ type_getattro(PyTypeObject *type, PyObject *name)
20312035

20322036
/* No attribute found in local __dict__ (or bases): use the
20332037
* descriptor from the metatype, if any */
2034-
if (meta_get != NULL)
2035-
return meta_get(meta_attribute, (PyObject *)type,
2036-
(PyObject *)metatype);
2038+
if (meta_get != NULL) {
2039+
PyObject *res;
2040+
res = meta_get(meta_attribute, (PyObject *)type,
2041+
(PyObject *)metatype);
2042+
Py_DECREF(meta_attribute);
2043+
return res;
2044+
}
20372045

20382046
/* If an ordinary attribute was found on the metatype, return it now */
20392047
if (meta_attribute != NULL) {
2040-
Py_INCREF(meta_attribute);
20412048
return meta_attribute;
20422049
}
20432050

0 commit comments

Comments
 (0)