Skip to content

Commit 670d78a

Browse files
Issue #24097: Fixed crash in object.__reduce__() if slot name is freed inside
__getattr__. Original patch by Antoine Pitrou.
2 parents 2cefc1e + d28bb62 commit 670d78a

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Lib/test/test_descr.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5092,6 +5092,23 @@ def __repr__(self):
50925092
objcopy2 = deepcopy(objcopy)
50935093
self._assert_is_copy(obj, objcopy2)
50945094

5095+
def test_issue24097(self):
5096+
# Slot name is freed inside __getattr__ and is later used.
5097+
class S(str): # Not interned
5098+
pass
5099+
class A:
5100+
__slotnames__ = [S('spam')]
5101+
def __getattr__(self, attr):
5102+
if attr == 'spam':
5103+
A.__slotnames__[:] = [S('spam')]
5104+
return 42
5105+
else:
5106+
raise AttributeError
5107+
5108+
import copyreg
5109+
expected = (copyreg.__newobj__, (A,), (None, {'spam': 42}), None, None)
5110+
self.assertEqual(A().__reduce__(2), expected) # Shouldn't crash
5111+
50955112

50965113
class SharedKeyTests(unittest.TestCase):
50975114

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: XXXX-XX-XX
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #24097: Fixed crash in object.__reduce__() if slot name is freed inside
14+
__getattr__.
15+
1316
- Issue #24731: Fixed crash on converting objects with special methods
1417
__bytes__, __trunc__, and __float__ returning instances of subclasses of
1518
bytes, int, and float to subclasses of bytes, int, and float correspondingly.

Objects/typeobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,8 +3889,10 @@ _PyObject_GetState(PyObject *obj)
38893889
PyObject *name, *value;
38903890

38913891
name = PyList_GET_ITEM(slotnames, i);
3892+
Py_INCREF(name);
38923893
value = PyObject_GetAttr(obj, name);
38933894
if (value == NULL) {
3895+
Py_DECREF(name);
38943896
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
38953897
goto error;
38963898
}
@@ -3899,6 +3901,7 @@ _PyObject_GetState(PyObject *obj)
38993901
}
39003902
else {
39013903
int err = PyDict_SetItem(slots, name, value);
3904+
Py_DECREF(name);
39023905
Py_DECREF(value);
39033906
if (err) {
39043907
goto error;

0 commit comments

Comments
 (0)