@@ -959,38 +959,35 @@ py_blake2_clear(PyObject *op)
959959 // initializes the HACL* internal state to NULL before allocating
960960 // it. If an error occurs in the constructor, we should only free
961961 // states that were allocated (i.e. that are not NULL).
962+ #define BLAKE2_FREE (TYPE , STATE ) \
963+ do { \
964+ if (STATE != NULL) { \
965+ Hacl_Hash_ ## TYPE ## _free(STATE); \
966+ STATE = NULL; \
967+ } \
968+ } while (0)
969+
962970 switch (self -> impl ) {
963971#if HACL_CAN_COMPILE_SIMD256
964972 case Blake2b_256 :
965- if (self -> blake2b_256_state != NULL ) {
966- Hacl_Hash_Blake2b_Simd256_free (self -> blake2b_256_state );
967- self -> blake2b_256_state = NULL ;
968- }
973+ BLAKE2_FREE (Blake2b_Simd256 , self -> blake2b_256_state );
969974 break ;
970975#endif
971976#if HACL_CAN_COMPILE_SIMD128
972977 case Blake2s_128 :
973- if (self -> blake2s_128_state != NULL ) {
974- Hacl_Hash_Blake2s_Simd128_free (self -> blake2s_128_state );
975- self -> blake2s_128_state = NULL ;
976- }
978+ BLAKE2_FREE (Blake2s_Simd128 , self -> blake2s_128_state );
977979 break ;
978980#endif
979981 case Blake2b :
980- if (self -> blake2b_state != NULL ) {
981- Hacl_Hash_Blake2b_free (self -> blake2b_state );
982- self -> blake2b_state = NULL ;
983- }
982+ BLAKE2_FREE (Blake2b , self -> blake2b_state );
984983 break ;
985984 case Blake2s :
986- if (self -> blake2s_state != NULL ) {
987- Hacl_Hash_Blake2s_free (self -> blake2s_state );
988- self -> blake2s_state = NULL ;
989- }
985+ BLAKE2_FREE (Blake2s , self -> blake2s_state );
990986 break ;
991987 default :
992988 Py_UNREACHABLE ();
993989 }
990+ #undef BLAKE2_FREE
994991 return 0 ;
995992}
996993
0 commit comments