@@ -1544,7 +1544,6 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
1544
1544
PyObject * digestmod )
1545
1545
/*[clinic end generated code: output=c20d9e4d9ed6d219 input=5f4071dcc7f34362]*/
1546
1546
{
1547
- PyTypeObject * type = get_hashlib_state (module )-> HMACtype ;
1548
1547
PY_EVP_MD * digest ;
1549
1548
HMAC_CTX * ctx = NULL ;
1550
1549
HMACobject * self = NULL ;
@@ -1557,8 +1556,8 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
1557
1556
}
1558
1557
1559
1558
if (digestmod == NULL ) {
1560
- PyErr_SetString (
1561
- PyExc_TypeError , "Missing required parameter 'digestmod'." );
1559
+ PyErr_SetString (PyExc_TypeError ,
1560
+ "Missing required parameter 'digestmod'." );
1562
1561
return NULL ;
1563
1562
}
1564
1563
@@ -1569,40 +1568,37 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
1569
1568
1570
1569
ctx = HMAC_CTX_new ();
1571
1570
if (ctx == NULL ) {
1572
- _setException ( PyExc_ValueError , NULL );
1571
+ PyErr_NoMemory ( );
1573
1572
goto error ;
1574
1573
}
1575
1574
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 */ );
1582
1576
PY_EVP_MD_free (digest );
1583
1577
if (r == 0 ) {
1584
1578
_setException (PyExc_ValueError , NULL );
1585
1579
goto error ;
1586
1580
}
1587
1581
1588
- self = (HMACobject * )PyObject_New (HMACobject , type );
1582
+ _hashlibstate * state = get_hashlib_state (module );
1583
+ self = PyObject_New (HMACobject , state -> HMACtype );
1589
1584
if (self == NULL ) {
1590
1585
goto error ;
1591
1586
}
1592
1587
1593
1588
self -> ctx = ctx ;
1594
1589
self -> lock = NULL ;
1590
+ ctx = NULL ; // 'ctx' is now owned by 'self'
1595
1591
1596
1592
if ((msg_obj != NULL ) && (msg_obj != Py_None )) {
1597
- if (!_hmac_update (self , msg_obj ))
1593
+ if (!_hmac_update (self , msg_obj )) {
1598
1594
goto error ;
1595
+ }
1599
1596
}
1600
-
1601
- return (PyObject * )self ;
1597
+ return (PyObject * )self ;
1602
1598
1603
1599
error :
1604
1600
if (ctx ) HMAC_CTX_free (ctx );
1605
- if ( self ) PyObject_Free (self );
1601
+ Py_XDECREF (self );
1606
1602
return NULL ;
1607
1603
}
1608
1604
@@ -1671,14 +1667,14 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
1671
1667
1672
1668
HMAC_CTX * ctx = HMAC_CTX_new ();
1673
1669
if (ctx == NULL ) {
1674
- return _setException ( PyExc_ValueError , NULL );
1670
+ return PyErr_NoMemory ( );
1675
1671
}
1676
1672
if (!locked_HMAC_CTX_copy (ctx , self )) {
1677
1673
HMAC_CTX_free (ctx );
1678
1674
return _setException (PyExc_ValueError , NULL );
1679
1675
}
1680
1676
1681
- retval = ( HMACobject * ) PyObject_New (HMACobject , Py_TYPE (self ));
1677
+ retval = PyObject_New (HMACobject , Py_TYPE (self ));
1682
1678
if (retval == NULL ) {
1683
1679
HMAC_CTX_free (ctx );
1684
1680
return NULL ;
@@ -1696,7 +1692,10 @@ _hmac_dealloc(HMACobject *self)
1696
1692
if (self -> lock != NULL ) {
1697
1693
PyThread_free_lock (self -> lock );
1698
1694
}
1699
- HMAC_CTX_free (self -> ctx );
1695
+ if (self -> ctx != NULL ) {
1696
+ HMAC_CTX_free (self -> ctx );
1697
+ self -> ctx = NULL ;
1698
+ }
1700
1699
PyObject_Free (self );
1701
1700
Py_DECREF (tp );
1702
1701
}
@@ -1741,6 +1740,7 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
1741
1740
return 0 ;
1742
1741
}
1743
1742
if (!locked_HMAC_CTX_copy (temp_ctx , self )) {
1743
+ HMAC_CTX_free (temp_ctx );
1744
1744
_setException (PyExc_ValueError , NULL );
1745
1745
return 0 ;
1746
1746
}
0 commit comments