@@ -1544,7 +1544,6 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15441544 PyObject * digestmod )
15451545/*[clinic end generated code: output=c20d9e4d9ed6d219 input=5f4071dcc7f34362]*/
15461546{
1547- PyTypeObject * type = get_hashlib_state (module )-> HMACtype ;
15481547 PY_EVP_MD * digest ;
15491548 HMAC_CTX * ctx = NULL ;
15501549 HMACobject * self = NULL ;
@@ -1557,8 +1556,8 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15571556 }
15581557
15591558 if (digestmod == NULL ) {
1560- PyErr_SetString (
1561- PyExc_TypeError , "Missing required parameter 'digestmod'." );
1559+ PyErr_SetString (PyExc_TypeError ,
1560+ "Missing required parameter 'digestmod'." );
15621561 return NULL ;
15631562 }
15641563
@@ -1569,40 +1568,37 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15691568
15701569 ctx = HMAC_CTX_new ();
15711570 if (ctx == NULL ) {
1572- _setException ( PyExc_ValueError , NULL );
1571+ PyErr_NoMemory ( );
15731572 goto error ;
15741573 }
15751574
1576- r = HMAC_Init_ex (
1577- ctx ,
1578- (const char * )key -> buf ,
1579- (int )key -> len ,
1580- digest ,
1581- NULL /*impl*/ );
1575+ r = HMAC_Init_ex (ctx , key -> buf , (int )key -> len , digest , NULL /* impl */ );
15821576 PY_EVP_MD_free (digest );
15831577 if (r == 0 ) {
15841578 _setException (PyExc_ValueError , NULL );
15851579 goto error ;
15861580 }
15871581
1588- self = (HMACobject * )PyObject_New (HMACobject , type );
1582+ _hashlibstate * state = get_hashlib_state (module );
1583+ self = PyObject_New (HMACobject , state -> HMACtype );
15891584 if (self == NULL ) {
15901585 goto error ;
15911586 }
15921587
15931588 self -> ctx = ctx ;
15941589 self -> lock = NULL ;
1590+ ctx = NULL ; // 'ctx' is now owned by 'self'
15951591
15961592 if ((msg_obj != NULL ) && (msg_obj != Py_None )) {
1597- if (!_hmac_update (self , msg_obj ))
1593+ if (!_hmac_update (self , msg_obj )) {
15981594 goto error ;
1595+ }
15991596 }
1600-
1601- return (PyObject * )self ;
1597+ return (PyObject * )self ;
16021598
16031599error :
16041600 if (ctx ) HMAC_CTX_free (ctx );
1605- if ( self ) PyObject_Free (self );
1601+ Py_XDECREF (self );
16061602 return NULL ;
16071603}
16081604
@@ -1671,14 +1667,14 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
16711667
16721668 HMAC_CTX * ctx = HMAC_CTX_new ();
16731669 if (ctx == NULL ) {
1674- return _setException ( PyExc_ValueError , NULL );
1670+ return PyErr_NoMemory ( );
16751671 }
16761672 if (!locked_HMAC_CTX_copy (ctx , self )) {
16771673 HMAC_CTX_free (ctx );
16781674 return _setException (PyExc_ValueError , NULL );
16791675 }
16801676
1681- retval = ( HMACobject * ) PyObject_New (HMACobject , Py_TYPE (self ));
1677+ retval = PyObject_New (HMACobject , Py_TYPE (self ));
16821678 if (retval == NULL ) {
16831679 HMAC_CTX_free (ctx );
16841680 return NULL ;
@@ -1696,7 +1692,10 @@ _hmac_dealloc(HMACobject *self)
16961692 if (self -> lock != NULL ) {
16971693 PyThread_free_lock (self -> lock );
16981694 }
1699- HMAC_CTX_free (self -> ctx );
1695+ if (self -> ctx != NULL ) {
1696+ HMAC_CTX_free (self -> ctx );
1697+ self -> ctx = NULL ;
1698+ }
17001699 PyObject_Free (self );
17011700 Py_DECREF (tp );
17021701}
@@ -1741,6 +1740,7 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
17411740 return 0 ;
17421741 }
17431742 if (!locked_HMAC_CTX_copy (temp_ctx , self )) {
1743+ HMAC_CTX_free (temp_ctx );
17441744 _setException (PyExc_ValueError , NULL );
17451745 return 0 ;
17461746 }
0 commit comments