Skip to content

Commit 5c50b6a

Browse files
[3.15] gh-159739: Keep descriptor metadata dictionaries alive during insertion (GH-160193) (#160382)
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 310c445 commit 5c50b6a

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
@@ -1465,8 +1465,9 @@ descriptor_set_wrapped_attribute(PyObject *oobj, PyObject *name, PyObject *value
14651465
return 0;
14661466
}
14671467
else {
1468+
int result = PyDict_SetItem(dict, name, value);
14681469
Py_DECREF(dict);
1469-
return PyDict_SetItem(dict, name, value);
1470+
return result;
14701471
}
14711472
}
14721473

0 commit comments

Comments
 (0)