@@ -390,33 +390,39 @@ narrow_hmac_hash_kind(hmacmodule_state *state, HMAC_Hash_Kind kind)
390390static int
391391_hacl_convert_errno (hacl_errno_t code )
392392{
393+ int res = -1 ;
394+ PyGILState_STATE gstate = PyGILState_Ensure ();
393395 switch (code ) {
394396 case Hacl_Streaming_Types_Success : {
395- return 0 ;
397+ res = 0 ;
398+ goto finally ;
396399 }
397400 case Hacl_Streaming_Types_InvalidAlgorithm : {
398- PyErr_Format (PyExc_ValueError , "invalid HACL* algorithm" );
399- return -1 ;
401+ PyErr_SetString (PyExc_ValueError , "invalid HACL* algorithm" );
402+ goto finally ;
400403 }
401404 case Hacl_Streaming_Types_InvalidLength : {
402405 PyErr_SetString (PyExc_ValueError , "invalid length" );
403- return -1 ;
406+ goto finally ;
404407 }
405408 case Hacl_Streaming_Types_MaximumLengthExceeded : {
406409 PyErr_SetString (PyExc_OverflowError , "maximum length exceeded" );
407- return -1 ;
410+ goto finally ;
408411 }
409412 case Hacl_Streaming_Types_OutOfMemory : {
410413 PyErr_NoMemory ();
411- return -1 ;
414+ goto finally ;
412415 }
413416 default : {
414417 PyErr_Format (PyExc_RuntimeError ,
415418 "HACL* internal routine failed with error code: %d" ,
416419 code );
417- return -1 ;
420+ goto finally ;
418421 }
419422 }
423+ finally :
424+ PyGILState_Release (gstate );
425+ return res ;
420426}
421427
422428/*
@@ -483,7 +489,7 @@ _hacl_hmac_state_update(HACL_HMAC_state *state, uint8_t *buf, Py_ssize_t len)
483489 assert (len >= 0 );
484490#ifdef Py_HMAC_SSIZE_LARGER_THAN_UINT32
485491 while (len > UINT32_MAX_AS_SSIZE_T ) {
486- if (_hacl_hmac_state_update_once (state , buf , UINT32_MAX )) {
492+ if (_hacl_hmac_state_update_once (state , buf , UINT32_MAX ) < 0 ) {
487493 assert (PyErr_Occurred ());
488494 return -1 ;
489495 }
@@ -492,7 +498,9 @@ _hacl_hmac_state_update(HACL_HMAC_state *state, uint8_t *buf, Py_ssize_t len)
492498 }
493499#endif
494500 if (len > UINT32_MAX_AS_SSIZE_T ) {
501+ PyGILState_STATE gstate = PyGILState_Ensure ();
495502 PyErr_Format (PyExc_ValueError , "invalid length: %zd (max: %ju)" , len , UINT32_MAX );
503+ PyGILState_Release (gstate );
496504 return -1 ;
497505 }
498506 return _hacl_hmac_state_update_once (state , buf , len );
@@ -781,13 +789,9 @@ _hmac_new_impl(PyObject *module, PyObject *keyobj, PyObject *msgobj,
781789 rc = _hacl_hmac_state_update (self -> state , msg .buf , msg .len )
782790 );
783791 PyBuffer_Release (& msg );
784- #ifndef NDEBUG
785792 if (rc < 0 ) {
786793 goto error ;
787794 }
788- #else
789- (void )rc ;
790- #endif
791795 }
792796 assert (rc == 0 );
793797 PyObject_GC_Track (self );
0 commit comments