From 1b51f411b1d59d738b2b349ce8b1c50e099b5584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 4 Jul 2026 12:09:42 +0200 Subject: [PATCH 1/3] [3.14] gh-152851: fix a crash when copying a BLAKE-2s/2b object (GH-153008) (cherry picked from commit 5a400ceafbbd4456ac65fbdbcf5d62ce9e8e4f9f) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../Library/2026-07-04-11-14-06.gh-issue-152851.diYvTf.rst | 2 ++ Modules/blake2module.c | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-04-11-14-06.gh-issue-152851.diYvTf.rst diff --git a/Misc/NEWS.d/next/Library/2026-07-04-11-14-06.gh-issue-152851.diYvTf.rst b/Misc/NEWS.d/next/Library/2026-07-04-11-14-06.gh-issue-152851.diYvTf.rst new file mode 100644 index 00000000000000..aab033ebeed695 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-04-11-14-06.gh-issue-152851.diYvTf.rst @@ -0,0 +1,2 @@ +Prevent a crash when allocation fails while copying a :func:`BLAKE-2s/2b +` object. Patch by Bénédikt Tran. diff --git a/Modules/blake2module.c b/Modules/blake2module.c index e31fa8131f1ecf..19ef7f9697e2cb 100644 --- a/Modules/blake2module.c +++ b/Modules/blake2module.c @@ -732,6 +732,9 @@ static int blake2_blake2b_copy_locked(Blake2Object *self, Blake2Object *cpy) { assert(cpy != NULL); + // Ensure that the implementation type is consistent with the HACL* state. + // See https://github.com/python/cpython/issues/152851 for details. + cpy->impl = self->impl; switch (self->impl) { #if _Py_HACL_CAN_COMPILE_VEC256 case Blake2b_256: { @@ -768,7 +771,6 @@ blake2_blake2b_copy_locked(Blake2Object *self, Blake2Object *cpy) default: Py_UNREACHABLE(); } - cpy->impl = self->impl; return 0; error: From fc69eff0319fbd6fb4e425e54d4edc1c87705083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 4 Jul 2026 12:13:23 +0200 Subject: [PATCH 2/3] Apply suggestions from code review --- Modules/blake2module.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/blake2module.c b/Modules/blake2module.c index 19ef7f9697e2cb..88c7893b2a2d4b 100644 --- a/Modules/blake2module.c +++ b/Modules/blake2module.c @@ -735,6 +735,7 @@ blake2_blake2b_copy_locked(Blake2Object *self, Blake2Object *cpy) // Ensure that the implementation type is consistent with the HACL* state. // See https://github.com/python/cpython/issues/152851 for details. cpy->impl = self->impl; + switch (self->impl) { #if _Py_HACL_CAN_COMPILE_VEC256 case Blake2b_256: { From 3452fb23fe60f0615396e75d56492ac89ce36a22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 4 Jul 2026 12:14:08 +0200 Subject: [PATCH 3/3] Update Modules/blake2module.c --- Modules/blake2module.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/blake2module.c b/Modules/blake2module.c index 88c7893b2a2d4b..c4fbd204d566d7 100644 --- a/Modules/blake2module.c +++ b/Modules/blake2module.c @@ -732,6 +732,7 @@ static int blake2_blake2b_copy_locked(Blake2Object *self, Blake2Object *cpy) { assert(cpy != NULL); + // Ensure that the implementation type is consistent with the HACL* state. // See https://github.com/python/cpython/issues/152851 for details. cpy->impl = self->impl;