@@ -1556,66 +1556,55 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15561556                       PyObject  * digestmod )
15571557/*[clinic end generated code: output=c20d9e4d9ed6d219 input=5f4071dcc7f34362]*/ 
15581558{
1559-     PyTypeObject  * type  =  get_hashlib_state (module )-> HMACtype ;
1560-     PY_EVP_MD  * digest ;
1561-     HMAC_CTX  * ctx  =  NULL ;
1562-     HMACobject  * self  =  NULL ;
1563-     int  r ;
1564- 
15651559    if  (key -> len  >  INT_MAX ) {
15661560        PyErr_SetString (PyExc_OverflowError ,
15671561                        "key is too long." );
15681562        return  NULL ;
15691563    }
15701564
15711565    if  (digestmod  ==  NULL ) {
1572-         PyErr_SetString (
1573-             PyExc_TypeError ,  "Missing required parameter 'digestmod'." );
1566+         PyErr_SetString (PyExc_TypeError , 
1567+                          "Missing required parameter 'digestmod'." );
15741568        return  NULL ;
15751569    }
15761570
1577-     digest  =  py_digest_by_digestmod (module , digestmod , Py_ht_mac );
1571+     PY_EVP_MD   * digest  =  py_digest_by_digestmod (module , digestmod , Py_ht_mac );
15781572    if  (digest  ==  NULL ) {
15791573        return  NULL ;
15801574    }
15811575
1582-     ctx  =  HMAC_CTX_new ();
1576+     HMAC_CTX   * ctx  =  HMAC_CTX_new ();
15831577    if  (ctx  ==  NULL ) {
15841578        _setException (PyExc_ValueError , NULL );
1585-         goto  error ;
1579+         return   NULL ;
15861580    }
15871581
1588-     r  =  HMAC_Init_ex (
1589-         ctx ,
1590-         (const  char * )key -> buf ,
1591-         (int )key -> len ,
1592-         digest ,
1593-         NULL  /*impl*/ );
1582+     int  ok  =  HMAC_Init_ex (ctx , key -> buf , (int )key -> len , digest , NULL );
15941583    PY_EVP_MD_free (digest );
1595-     if  (r  ==  0 ) {
1584+     if  (!ok ) {
1585+         HMAC_CTX_free (ctx );
15961586        _setException (PyExc_ValueError , NULL );
1597-         goto  error ;
1587+         return   NULL ;
15981588    }
15991589
1600-     self  =  (HMACobject  * )PyObject_New (HMACobject , type );
1590+     _hashlibstate  * state  =  get_hashlib_state (module );
1591+     HMACobject  * self  =  PyObject_New (HMACobject , state -> HMACtype );
16011592    if  (self  ==  NULL ) {
1602-         goto error ;
1593+         HMAC_CTX_free (ctx );
1594+         return  NULL ;
16031595    }
16041596
16051597    self -> ctx  =  ctx ;
16061598    HASHLIB_INIT_MUTEX (self );
16071599
16081600    if  ((msg_obj  !=  NULL ) &&  (msg_obj  !=  Py_None )) {
1609-         if  (!_hmac_update (self , msg_obj ))
1610-             goto error ;
1601+         if  (!_hmac_update (self , msg_obj )) {
1602+             Py_DECREF (self );  // this also frees the HMAC context 
1603+             return  NULL ;
1604+         }
16111605    }
16121606
1613-     return  (PyObject * )self ;
1614- 
1615- error :
1616-     if  (ctx ) HMAC_CTX_free (ctx );
1617-     if  (self ) PyObject_Free (self );
1618-     return  NULL ;
1607+     return  (PyObject  * )self ;
16191608}
16201609
16211610/* helper functions */ 
@@ -1703,7 +1692,10 @@ static void
17031692_hmac_dealloc (HMACobject  * self )
17041693{
17051694    PyTypeObject  * tp  =  Py_TYPE (self );
1706-     HMAC_CTX_free (self -> ctx );
1695+     if  (self -> ctx  !=  NULL ) {
1696+         HMAC_CTX_free (self -> ctx );
1697+         self -> ctx  =  NULL ;
1698+     }
17071699    PyObject_Free (self );
17081700    Py_DECREF (tp );
17091701}
0 commit comments