Skip to content

Commit e031e9b

Browse files
committed
gh-159739: Keep descriptor metadata dictionaries alive during insertion
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.
1 parent d4750a7 commit e031e9b

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
@@ -1460,8 +1460,9 @@ descriptor_set_wrapped_attribute(PyObject *oobj, PyObject *name, PyObject *value
14601460
return 0;
14611461
}
14621462
else {
1463+
int result = PyDict_SetItem(dict, name, value);
14631464
Py_DECREF(dict);
1464-
return PyDict_SetItem(dict, name, value);
1465+
return result;
14651466
}
14661467
}
14671468

0 commit comments

Comments
 (0)