if (task->task_must_cancel) {
PyObject *r;
int is_true;
+
+ // Beware: An evil `__getattribute__` could
+ // prematurely delete task->task_cancel_msg before the
+ // task is cancelled, thereby causing a UAF crash.
+ //
+ // See https://github.com/python/cpython/issues/126138
+ PyObject *task_cancel_msg = Py_NewRef(task->task_cancel_msg);
r = PyObject_CallMethodOneArg(result, &_Py_ID(cancel),
- task->task_cancel_msg);
+ task_cancel_msg);
+ Py_DECREF(task_cancel_msg);
+
if (r == NULL) {
return NULL;
}
if (task->task_must_cancel) {
PyObject *r;
int is_true;
+
+ // Beware: An evil `__getattribute__` could
+ // prematurely delete task->task_cancel_msg before the
+ // task is cancelled, thereby causing a UAF crash.
+ //
+ // See https://github.com/python/cpython/issues/126138
+ PyObject *task_cancel_msg = Py_NewRef(task->task_cancel_msg);
r = PyObject_CallMethodOneArg(result, &_Py_ID(cancel),
- task->task_cancel_msg);
+ task_cancel_msg);
+ Py_DECREF(task_cancel_msg);
+
if (r == NULL) {
return NULL;
}