Skip to content

Commit c0bc2c0

Browse files
authored
[3.14] gh-155830: properly initialize BLAKE-2 objects before GC tracking (GH-155831) (#159742)
(cherry picked from commit 02844c8)
1 parent a436cd2 commit c0bc2c0

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`!_blake2`: Ensure that BLAKE-2 objects are correctly initialized
2+
before being tracked by the garbage collector. Patch by Bénédikt Tran.

‎Modules/blake2module.c‎

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,29 @@ new_Blake2Object(PyTypeObject *type)
388388
return NULL;
389389
}
390390
HASHLIB_INIT_MUTEX(self);
391+
self->impl = type_to_impl(type);
392+
// Ensure that the states are NULL-initialized in case of an error.
393+
// See: py_blake2_clear() for more details.
394+
switch (self->impl) {
395+
#if _Py_HACL_CAN_COMPILE_VEC256
396+
case Blake2b_256:
397+
self->blake2b_256_state = NULL;
398+
break;
399+
#endif
400+
#if _Py_HACL_CAN_COMPILE_VEC128
401+
case Blake2s_128:
402+
self->blake2s_128_state = NULL;
403+
break;
404+
#endif
405+
case Blake2b:
406+
self->blake2b_state = NULL;
407+
break;
408+
case Blake2s:
409+
self->blake2s_state = NULL;
410+
break;
411+
default:
412+
Py_UNREACHABLE();
413+
}
391414

392415
PyObject_GC_Track(self);
393416
return self;
@@ -464,29 +487,6 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
464487
goto error;
465488
}
466489

467-
self->impl = type_to_impl(type);
468-
// Ensure that the states are NULL-initialized in case of an error.
469-
// See: py_blake2_clear() for more details.
470-
switch (self->impl) {
471-
#if _Py_HACL_CAN_COMPILE_VEC256
472-
case Blake2b_256:
473-
self->blake2b_256_state = NULL;
474-
break;
475-
#endif
476-
#if _Py_HACL_CAN_COMPILE_VEC128
477-
case Blake2s_128:
478-
self->blake2s_128_state = NULL;
479-
break;
480-
#endif
481-
case Blake2b:
482-
self->blake2b_state = NULL;
483-
break;
484-
case Blake2s:
485-
self->blake2s_state = NULL;
486-
break;
487-
default:
488-
Py_UNREACHABLE();
489-
}
490490
// Using Blake2b because we statically know that these are greater than the
491491
// Blake2s sizes -- this avoids a VLA.
492492
uint8_t salt_[HACL_HASH_BLAKE2B_SALT_BYTES] = { 0 };

0 commit comments

Comments
 (0)