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

Skip to content

Commit 64aafeb

Browse files
committed
Issue #16447: Fix potential segfault when setting __name__ on a class.
1 parent eff6444 commit 64aafeb

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

Lib/test/test_descr.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3997,6 +3997,20 @@ class C(object):
39973997
C.__name__ = 'D.E'
39983998
self.assertEqual((C.__module__, C.__name__), (mod, 'D.E'))
39993999

4000+
def test_evil_type_name(self):
4001+
# A badly placed Py_DECREF in type_set_name led to arbitrary code
4002+
# execution while the type structure was not in a sane state, and a
4003+
# possible segmentation fault as a result. See bug #16447.
4004+
class Nasty(str):
4005+
def __del__(self):
4006+
C.__name__ = "other"
4007+
4008+
class C:
4009+
pass
4010+
4011+
C.__name__ = Nasty("abc")
4012+
C.__name__ = "normal"
4013+
40004014
def test_subclass_right_op(self):
40014015
# Testing correct dispatch of subclass overloading __r<op>__...
40024016

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.3.2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #16447: Fixed potential segmentation fault when setting __name__ on a
16+
class.
17+
1518
- Issue #17669: Fix crash involving finalization of generators using yield from.
1619

1720
- Issue #17619: Make input() check for Ctrl-C correctly on Windows.

Objects/typeobject.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,13 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context)
298298

299299
Py_INCREF(value);
300300

301-
Py_DECREF(et->ht_name);
301+
/* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name
302+
value. (Bug #16447.) */
303+
tmp = et->ht_name;
302304
et->ht_name = value;
303305

304306
type->tp_name = tp_name;
307+
Py_DECREF(tmp);
305308

306309
return 0;
307310
}

0 commit comments

Comments
 (0)