Skip to content

Commit 4dff6f6

Browse files
committed
Issue9951: update _hashopenssl and md5module to use _Py_strhex().
Also update _posixsubprocess to use Py_hexdigits instead of its own constant.
1 parent 8cb6569 commit 4dff6f6

3 files changed

Lines changed: 6 additions & 41 deletions

File tree

‎Modules/_hashopenssl.c‎

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "Python.h"
1717
#include "structmember.h"
1818
#include "hashlib.h"
19+
#include "pystrhex.h"
1920

2021

2122
/* EVP is the preferred interface to hashing in OpenSSL */
@@ -157,9 +158,7 @@ EVP_hexdigest(EVPobject *self, PyObject *unused)
157158
{
158159
unsigned char digest[EVP_MAX_MD_SIZE];
159160
EVP_MD_CTX temp_ctx;
160-
PyObject *retval;
161-
char *hex_digest;
162-
unsigned int i, j, digest_size;
161+
unsigned int digest_size;
163162

164163
/* Get the raw (binary) digest value */
165164
locked_EVP_MD_CTX_copy(&temp_ctx, self);
@@ -168,22 +167,7 @@ EVP_hexdigest(EVPobject *self, PyObject *unused)
168167

169168
EVP_MD_CTX_cleanup(&temp_ctx);
170169

171-
/* Allocate a new buffer */
172-
hex_digest = PyMem_Malloc(digest_size * 2 + 1);
173-
if (!hex_digest)
174-
return PyErr_NoMemory();
175-
176-
/* Make hex version of the digest */
177-
for(i=j=0; i<digest_size; i++) {
178-
unsigned char c;
179-
c = (digest[i] >> 4) & 0xf;
180-
hex_digest[j++] = Py_hexdigits[c];
181-
c = (digest[i] & 0xf);
182-
hex_digest[j++] = Py_hexdigits[c];
183-
}
184-
retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2);
185-
PyMem_Free(hex_digest);
186-
return retval;
170+
return _Py_strhex((const char *)digest, digest_size);
187171
}
188172

189173
PyDoc_STRVAR(EVP_update__doc__,

‎Modules/_posixsubprocess.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ child_exec(char *const exec_array[],
504504
_Py_write_noraise(errpipe_write, "OSError:", 8);
505505
cur = hex_errno + sizeof(hex_errno);
506506
while (saved_errno != 0 && cur > hex_errno) {
507-
*--cur = "0123456789ABCDEF"[saved_errno % 16];
507+
*--cur = Py_hexdigits[saved_errno % 16];
508508
saved_errno /= 16;
509509
}
510510
_Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);

‎Modules/md5module.c‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "Python.h"
2020
#include "hashlib.h"
21+
#include "pystrhex.h"
2122

2223
/*[clinic input]
2324
module _md5
@@ -387,32 +388,12 @@ MD5Type_hexdigest_impl(MD5object *self)
387388
{
388389
unsigned char digest[MD5_DIGESTSIZE];
389390
struct md5_state temp;
390-
PyObject *retval;
391-
Py_UCS1 *hex_digest;
392-
int i, j;
393391

394392
/* Get the raw (binary) digest value */
395393
temp = self->hash_state;
396394
md5_done(&temp, digest);
397395

398-
/* Create a new string */
399-
retval = PyUnicode_New(MD5_DIGESTSIZE * 2, 127);
400-
if (!retval)
401-
return NULL;
402-
hex_digest = PyUnicode_1BYTE_DATA(retval);
403-
404-
/* Make hex version of the digest */
405-
for(i=j=0; i<MD5_DIGESTSIZE; i++) {
406-
unsigned char c;
407-
c = (digest[i] >> 4) & 0xf;
408-
hex_digest[j++] = Py_hexdigits[c];
409-
c = (digest[i] & 0xf);
410-
hex_digest[j++] = Py_hexdigits[c];
411-
}
412-
#ifdef Py_DEBUG
413-
assert(_PyUnicode_CheckConsistency(retval, 1));
414-
#endif
415-
return retval;
396+
return _Py_strhex((const char*)digest, MD5_DIGESTSIZE);
416397
}
417398

418399
/*[clinic input]

0 commit comments

Comments
 (0)