From 7f817f58e15b6d1bb67f93f8a9947c5dfd05e10c 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, 15 Aug 2026 11:49:49 +0200 Subject: [PATCH] gh-155830: properly initialize BLAKE-2 objects before GC tracking --- ...-08-15-11-49-30.gh-issue-155830.cY5xgK.rst | 2 + Modules/blake2module.c | 47 +++++++++---------- 2 files changed, 25 insertions(+), 24 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-08-15-11-49-30.gh-issue-155830.cY5xgK.rst diff --git a/Misc/NEWS.d/next/Library/2026-08-15-11-49-30.gh-issue-155830.cY5xgK.rst b/Misc/NEWS.d/next/Library/2026-08-15-11-49-30.gh-issue-155830.cY5xgK.rst new file mode 100644 index 000000000000000..9d6c2a82903d027 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-15-11-49-30.gh-issue-155830.cY5xgK.rst @@ -0,0 +1,2 @@ +:mod:`!_blake2`: Ensure that BLAKE-2 objects are correctly initialized +before being tracked by the garbage collector. Patch by Bénédikt Tran. diff --git a/Modules/blake2module.c b/Modules/blake2module.c index ac7265bb9d6836c..5ea64cb28885db9 100644 --- a/Modules/blake2module.c +++ b/Modules/blake2module.c @@ -392,6 +392,29 @@ new_Blake2Object(PyTypeObject *type) return NULL; } HASHLIB_INIT_MUTEX(self); + self->impl = type_to_impl(type); + // Ensure that the states are NULL-initialized in case of an error. + // See: py_blake2_clear() for more details. + switch (self->impl) { +#if _Py_HACL_CAN_COMPILE_VEC256 + case Blake2b_256: + self->blake2b_256_state = NULL; + break; +#endif +#if _Py_HACL_CAN_COMPILE_VEC128 + case Blake2s_128: + self->blake2s_128_state = NULL; + break; +#endif + case Blake2b: + self->blake2b_state = NULL; + break; + case Blake2s: + self->blake2s_state = NULL; + break; + default: + Py_UNREACHABLE(); + } PyObject_GC_Track(self); return self; @@ -553,30 +576,6 @@ py_blake2_new(PyTypeObject *type, PyObject *data, int digest_size, goto error; } - self->impl = type_to_impl(type); - // Ensure that the states are NULL-initialized in case of an error. - // See: py_blake2_clear() for more details. - switch (self->impl) { -#if _Py_HACL_CAN_COMPILE_VEC256 - case Blake2b_256: - self->blake2b_256_state = NULL; - break; -#endif -#if _Py_HACL_CAN_COMPILE_VEC128 - case Blake2s_128: - self->blake2s_128_state = NULL; - break; -#endif - case Blake2b: - self->blake2b_state = NULL; - break; - case Blake2s: - self->blake2s_state = NULL; - break; - default: - Py_UNREACHABLE(); - } - // Unlike the state types, the parameters share a single (client-friendly) // structure. if (py_blake2_validate_params(self,