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