https://github.com/python/cpython/commit/a1f31af280a5c56c9eb85e2de5251c3c1b0e8bd3
commit: a1f31af280a5c56c9eb85e2de5251c3c1b0e8bd3
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: sobolevn <[email protected]>
date: 2026-06-23T14:39:29Z
summary:

[3.13] gh-151773: Fix NULL dereference in `PyContextVar_Set` (GH-151836) 
(#152011)

gh-151773: Fix NULL dereference in `PyContextVar_Set` (GH-151836)
(cherry picked from commit d35b1719a58c8682a22ef698e070e97661af8d14)

Co-authored-by: dev <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst
M Python/context.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst
new file mode 100644
index 000000000000000..b4193c5e15ffef8
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-21-16-00-00.gh-issue-151773.mN4kRt.rst
@@ -0,0 +1,2 @@
+Fix a crash in :func:`contextvars.ContextVar.set` when memory allocation
+fails.
diff --git a/Python/context.c b/Python/context.c
index bc8b45cf6f1d5b0..5c48ef7a138b125 100644
--- a/Python/context.c
+++ b/Python/context.c
@@ -278,6 +278,9 @@ PyContextVar_Set(PyObject *ovar, PyObject *val)
     Py_XINCREF(old_val);
     PyContextToken *tok = token_new(ctx, var, old_val);
     Py_XDECREF(old_val);
+    if (tok == NULL) {
+        return NULL;
+    }
 
     if (contextvar_set(var, val)) {
         Py_DECREF(tok);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to