https://github.com/python/cpython/commit/c0bc2c03246cb8e1572b2099d1d224c61eada270
commit: c0bc2c03246cb8e1572b2099d1d224c61eada270
branch: 3.14
author: Bénédikt Tran <[email protected]>
committer: picnixz <[email protected]>
date: 2026-10-19T21:23:22Z
summary:

[3.14] gh-155830: properly initialize BLAKE-2 objects before GC tracking 
(GH-155831) (#159742)

(cherry picked from commit 02844c83b08873ba5871175fdd9d909ae0ed0f4f)

files:
A Misc/NEWS.d/next/Library/2026-08-15-11-49-30.gh-issue-155830.cY5xgK.rst
M Modules/blake2module.c

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 c4fbd204d566d70..957fa73f686180b 100644
--- a/Modules/blake2module.c
+++ b/Modules/blake2module.c
@@ -388,6 +388,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;
@@ -464,29 +487,6 @@ py_blake2b_or_s_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();
-    }
     // Using Blake2b because we statically know that these are greater than the
     // Blake2s sizes -- this avoids a VLA.
     uint8_t salt_[HACL_HASH_BLAKE2B_SALT_BYTES] = { 0 };

_______________________________________________
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