Skip to content

Commit 0078517

Browse files
committed
gh-129813, PEP 782: Use PyBytesWriter in _hashopenssl
Replace PyBytes_FromStringAndSize(NULL, size) with the new public PyBytesWriter API.
1 parent c3fca5d commit 0078517

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

Modules/_hashopenssl.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,39 +1101,39 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
11011101
/*[clinic end generated code: output=dcb09335dd2fe908 input=224d047da2c12a42]*/
11021102
{
11031103
EVP_MD_CTX *temp_ctx;
1104-
PyObject *retval;
11051104

11061105
if (length == 0) {
11071106
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
11081107
}
11091108

1110-
retval = PyBytes_FromStringAndSize(NULL, length);
1111-
if (retval == NULL) {
1109+
PyBytesWriter *writer = PyBytesWriter_Create(length);
1110+
if (writer == NULL) {
11121111
return NULL;
11131112
}
11141113

11151114
temp_ctx = py_wrapper_EVP_MD_CTX_new();
11161115
if (temp_ctx == NULL) {
1117-
Py_DECREF(retval);
1116+
PyBytesWriter_Discard(writer);
1117+
PyErr_NoMemory();
11181118
return NULL;
11191119
}
11201120

11211121
if (_hashlib_HASH_copy_locked(self, temp_ctx) < 0) {
11221122
goto error;
11231123
}
11241124
if (!EVP_DigestFinalXOF(temp_ctx,
1125-
(unsigned char*)PyBytes_AS_STRING(retval),
1125+
(unsigned char*)PyBytesWriter_GetData(writer),
11261126
length))
11271127
{
11281128
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_DigestFinalXOF));
11291129
goto error;
11301130
}
11311131

11321132
EVP_MD_CTX_free(temp_ctx);
1133-
return retval;
1133+
return PyBytesWriter_Finish(writer);
11341134

11351135
error:
1136-
Py_DECREF(retval);
1136+
PyBytesWriter_Discard(writer);
11371137
EVP_MD_CTX_free(temp_ctx);
11381138
return NULL;
11391139
}
@@ -1750,7 +1750,6 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
17501750
long maxmem, long dklen)
17511751
/*[clinic end generated code: output=d424bc3e8c6b9654 input=bdeac9628d07f7a1]*/
17521752
{
1753-
PyObject *key = NULL;
17541753
int retval;
17551754

17561755
if (password->len > INT_MAX) {
@@ -1791,8 +1790,8 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
17911790
return NULL;
17921791
}
17931792

1794-
key = PyBytes_FromStringAndSize(NULL, dklen);
1795-
if (key == NULL) {
1793+
PyBytesWriter *writer = PyBytesWriter_Create(dklen);
1794+
if (writer == NULL) {
17961795
return NULL;
17971796
}
17981797

@@ -1801,16 +1800,16 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
18011800
(const char *)password->buf, (size_t)password->len,
18021801
(const unsigned char *)salt->buf, (size_t)salt->len,
18031802
(uint64_t)n, (uint64_t)r, (uint64_t)p, (uint64_t)maxmem,
1804-
(unsigned char *)PyBytes_AS_STRING(key), (size_t)dklen
1803+
PyBytesWriter_GetData(writer), (size_t)dklen
18051804
);
18061805
Py_END_ALLOW_THREADS
18071806

18081807
if (!retval) {
1809-
Py_DECREF(key);
1808+
PyBytesWriter_Discard(writer);
18101809
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_PBE_scrypt));
18111810
return NULL;
18121811
}
1813-
return key;
1812+
return PyBytesWriter_Finish(writer);
18141813
}
18151814

18161815
#undef HASHLIB_SCRYPT_MAX_DKLEN

0 commit comments

Comments
 (0)