Skip to content

Commit a2916fe

Browse files
[3.14] gh-159739: Keep descriptor metadata dictionaries alive during insertion (GH-160193) (#160383)
The classmethod and staticmethod metadata setters dropped their reference to the descriptor dictionary before inserting the new value. A key comparison or value finalizer can replace the dictionary during insertion, leaving the operation using released memory. Keep the dictionary alive until insertion finishes. Backport to 3.15 and 3.14. (cherry picked from commit a076a55) Co-authored-by: Jelle Zijlstra <jelle@openai.com>
1 parent 5924fd4 commit a2916fe

2 files changed

Lines changed: 3 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a possible crash when assigning metadata to :class:`classmethod` or :class:`staticmethod` objects.

‎Objects/funcobject.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1480,8 +1480,9 @@ descriptor_set_wrapped_attribute(PyObject *oobj, PyObject *name, PyObject *value
14801480
return 0;
14811481
}
14821482
else {
1483+
int result = PyDict_SetItem(dict, name, value);
14831484
Py_DECREF(dict);
1484-
return PyDict_SetItem(dict, name, value);
1485+
return result;
14851486
}
14861487
}
14871488

0 commit comments

Comments
 (0)