]> granicus.if.org Git - python/commitdiff
Issue #24097: Fixed crash in object.__reduce__() if slot name is freed inside
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 25 Nov 2015 16:33:29 +0000 (18:33 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 25 Nov 2015 16:33:29 +0000 (18:33 +0200)
__getattr__.  Original patch by Antoine Pitrou.

Lib/test/test_descr.py
Misc/NEWS
Objects/typeobject.c

index ce9626c77a77d2199914ac92111db89a623d6090..64a4a364f7314e6a8bcb0f5bbc114bdab0749f39 100644 (file)
@@ -4988,6 +4988,23 @@ class PicklingTests(unittest.TestCase):
                     objcopy2 = deepcopy(objcopy)
                     self._assert_is_copy(obj, objcopy2)
 
+    def test_issue24097(self):
+        # Slot name is freed inside __getattr__ and is later used.
+        class S(str):  # Not interned
+            pass
+        class A:
+            __slotnames__ = [S('spam')]
+            def __getattr__(self, attr):
+                if attr == 'spam':
+                    A.__slotnames__[:] = [S('spam')]
+                    return 42
+                else:
+                    raise AttributeError
+
+        import copyreg
+        expected = (copyreg.__newobj__, (A,), (None, {'spam': 42}), None, None)
+        self.assertEqual(A().__reduce__(2), expected)  # Shouldn't crash
+
 
 class SharedKeyTests(unittest.TestCase):
 
index fa365cc0dd0aaf2c09e4316c238cea2f78df17cf..9dc0aae2f5fcef52878e590cb4eaf8a99a7e2c6d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: tba
 Core and Builtins
 -----------------
 
+- Issue #24097: Fixed crash in object.__reduce__() if slot name is freed inside
+  __getattr__.
+
 - Issue #24731: Fixed crash on converting objects with special methods
   __bytes__, __trunc__, and __float__ returning instances of subclasses of
   bytes, int, and float to subclasses of bytes, int, and float correspondingly.
index b38e0fb450e423e8c3eb36dbb998c64fa4ede56d..ca5355a7dafc155e564954410b11aeebcb62fd3c 100644 (file)
@@ -3768,8 +3768,10 @@ _PyObject_GetState(PyObject *obj)
                 PyObject *name, *value;
 
                 name = PyList_GET_ITEM(slotnames, i);
+                Py_INCREF(name);
                 value = PyObject_GetAttr(obj, name);
                 if (value == NULL) {
+                    Py_DECREF(name);
                     if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
                         goto error;
                     }
@@ -3778,6 +3780,7 @@ _PyObject_GetState(PyObject *obj)
                 }
                 else {
                     int err = PyDict_SetItem(slots, name, value);
+                    Py_DECREF(name);
                     Py_DECREF(value);
                     if (err) {
                         goto error;