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 000000000000000..aab033ebeed6954 --- /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 e31fa8131f1ecf4..c4fbd204d566d70 100644 --- a/Modules/blake2module.c +++ b/Modules/blake2module.c @@ -732,6 +732,11 @@ 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 +773,6 @@ blake2_blake2b_copy_locked(Blake2Object *self, Blake2Object *cpy) default: Py_UNREACHABLE(); } - cpy->impl = self->impl; return 0; error: