From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Tue, 23 Jun 2026 14:41:31 +0000 (+0200) Subject: [3.14] gh-151773: Fix NULL dereference in `PyContextVar_Set` (GH-151836) (#152010) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b471b4a6bad62f0b237aa4cb86bf8f20481c129c;p=thirdparty%2FPython%2Fcpython.git [3.14] gh-151773: Fix NULL dereference in `PyContextVar_Set` (GH-151836) (#152010) gh-151773: Fix NULL dereference in `PyContextVar_Set` (GH-151836) (cherry picked from commit d35b1719a58c8682a22ef698e070e97661af8d14) Co-authored-by: dev --- 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 000000000000..b4193c5e15ff --- /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 0715612fbe79..9cec2eec1893 100644 --- a/Python/context.c +++ b/Python/context.c @@ -357,6 +357,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);