@@ -1556,7 +1556,6 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
1556
1556
PyObject * digestmod )
1557
1557
/*[clinic end generated code: output=c20d9e4d9ed6d219 input=5f4071dcc7f34362]*/
1558
1558
{
1559
- PyTypeObject * type = get_hashlib_state (module )-> HMACtype ;
1560
1559
PY_EVP_MD * digest ;
1561
1560
HMAC_CTX * ctx = NULL ;
1562
1561
HMACobject * self = NULL ;
@@ -1569,8 +1568,8 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
1569
1568
}
1570
1569
1571
1570
if (digestmod == NULL ) {
1572
- PyErr_SetString (
1573
- PyExc_TypeError , "Missing required parameter 'digestmod'." );
1571
+ PyErr_SetString (PyExc_TypeError ,
1572
+ "Missing required parameter 'digestmod'." );
1574
1573
return NULL ;
1575
1574
}
1576
1575
@@ -1581,40 +1580,37 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
1581
1580
1582
1581
ctx = HMAC_CTX_new ();
1583
1582
if (ctx == NULL ) {
1584
- _setException ( PyExc_ValueError , NULL );
1583
+ PyErr_NoMemory ( );
1585
1584
goto error ;
1586
1585
}
1587
1586
1588
- r = HMAC_Init_ex (
1589
- ctx ,
1590
- (const char * )key -> buf ,
1591
- (int )key -> len ,
1592
- digest ,
1593
- NULL /*impl*/ );
1587
+ r = HMAC_Init_ex (ctx , key -> buf , (int )key -> len , digest , NULL /* impl */ );
1594
1588
PY_EVP_MD_free (digest );
1595
1589
if (r == 0 ) {
1596
1590
_setException (PyExc_ValueError , NULL );
1597
1591
goto error ;
1598
1592
}
1599
1593
1600
- self = (HMACobject * )PyObject_New (HMACobject , type );
1594
+ _hashlibstate * state = get_hashlib_state (module );
1595
+ self = PyObject_New (HMACobject , state -> HMACtype );
1601
1596
if (self == NULL ) {
1602
1597
goto error ;
1603
1598
}
1604
1599
1605
1600
self -> ctx = ctx ;
1601
+ ctx = NULL ; // 'ctx' is now owned by 'self'
1606
1602
HASHLIB_INIT_MUTEX (self );
1607
1603
1608
1604
if ((msg_obj != NULL ) && (msg_obj != Py_None )) {
1609
- if (!_hmac_update (self , msg_obj ))
1605
+ if (!_hmac_update (self , msg_obj )) {
1610
1606
goto error ;
1607
+ }
1611
1608
}
1612
-
1613
- return (PyObject * )self ;
1609
+ return (PyObject * )self ;
1614
1610
1615
1611
error :
1616
1612
if (ctx ) HMAC_CTX_free (ctx );
1617
- if ( self ) PyObject_Free (self );
1613
+ Py_XDECREF (self );
1618
1614
return NULL ;
1619
1615
}
1620
1616
@@ -1681,14 +1677,14 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
1681
1677
1682
1678
HMAC_CTX * ctx = HMAC_CTX_new ();
1683
1679
if (ctx == NULL ) {
1684
- return _setException ( PyExc_ValueError , NULL );
1680
+ return PyErr_NoMemory ( );
1685
1681
}
1686
1682
if (!locked_HMAC_CTX_copy (ctx , self )) {
1687
1683
HMAC_CTX_free (ctx );
1688
1684
return _setException (PyExc_ValueError , NULL );
1689
1685
}
1690
1686
1691
- retval = ( HMACobject * ) PyObject_New (HMACobject , Py_TYPE (self ));
1687
+ retval = PyObject_New (HMACobject , Py_TYPE (self ));
1692
1688
if (retval == NULL ) {
1693
1689
HMAC_CTX_free (ctx );
1694
1690
return NULL ;
@@ -1703,7 +1699,10 @@ static void
1703
1699
_hmac_dealloc (HMACobject * self )
1704
1700
{
1705
1701
PyTypeObject * tp = Py_TYPE (self );
1706
- HMAC_CTX_free (self -> ctx );
1702
+ if (self -> ctx != NULL ) {
1703
+ HMAC_CTX_free (self -> ctx );
1704
+ self -> ctx = NULL ;
1705
+ }
1707
1706
PyObject_Free (self );
1708
1707
Py_DECREF (tp );
1709
1708
}
@@ -1748,6 +1747,7 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
1748
1747
return 0 ;
1749
1748
}
1750
1749
if (!locked_HMAC_CTX_copy (temp_ctx , self )) {
1750
+ HMAC_CTX_free (temp_ctx );
1751
1751
_setException (PyExc_ValueError , NULL );
1752
1752
return 0 ;
1753
1753
}
0 commit comments