Skip to content

Commit a68efdf

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in _hashopenssl (#138922)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public PyBytesWriter API.
1 parent 29d026f commit a68efdf

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Modules/_hashopenssl.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,6 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name,
16291629
{
16301630
_hashlibstate *state = get_hashlib_state(module);
16311631
PyObject *key_obj = NULL;
1632-
char *key;
16331632
long dklen;
16341633
int retval;
16351634

@@ -1682,24 +1681,24 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name,
16821681
goto end;
16831682
}
16841683

1685-
key_obj = PyBytes_FromStringAndSize(NULL, dklen);
1686-
if (key_obj == NULL) {
1684+
PyBytesWriter *writer = PyBytesWriter_Create(dklen);
1685+
if (writer == NULL) {
16871686
goto end;
16881687
}
1689-
key = PyBytes_AS_STRING(key_obj);
16901688

16911689
Py_BEGIN_ALLOW_THREADS
16921690
retval = PKCS5_PBKDF2_HMAC((const char *)password->buf, (int)password->len,
16931691
(const unsigned char *)salt->buf, (int)salt->len,
16941692
iterations, digest, dklen,
1695-
(unsigned char *)key);
1693+
(unsigned char *)PyBytesWriter_GetData(writer));
16961694
Py_END_ALLOW_THREADS
16971695

16981696
if (!retval) {
1699-
Py_CLEAR(key_obj);
1697+
PyBytesWriter_Discard(writer);
17001698
notify_ssl_error_occurred_in(Py_STRINGIFY(PKCS5_PBKDF2_HMAC));
17011699
goto end;
17021700
}
1701+
key_obj = PyBytesWriter_Finish(writer);
17031702

17041703
end:
17051704
if (digest != NULL) {
@@ -1799,7 +1798,7 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
17991798
(const char *)password->buf, (size_t)password->len,
18001799
(const unsigned char *)salt->buf, (size_t)salt->len,
18011800
(uint64_t)n, (uint64_t)r, (uint64_t)p, (uint64_t)maxmem,
1802-
PyBytesWriter_GetData(writer), (size_t)dklen
1801+
(unsigned char *)PyBytesWriter_GetData(writer), (size_t)dklen
18031802
);
18041803
Py_END_ALLOW_THREADS
18051804

0 commit comments

Comments
 (0)