Skip to content

Commit 0d7cb5b

Browse files
bpo-38132: Check EVP_DigestUpdate for error (GH-16041)
(cherry picked from commit 8c74574) Co-authored-by: Christian Heimes <[email protected]>
1 parent 2f01cf6 commit 0d7cb5b

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Modules/_hashopenssl.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ newEVPobject(void)
9696
return retval;
9797
}
9898

99-
static void
99+
static int
100100
EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
101101
{
102102
unsigned int process;
@@ -108,11 +108,12 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
108108
process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
109109
if (!EVP_DigestUpdate(self->ctx, (const void*)cp, process)) {
110110
_setException(PyExc_ValueError);
111-
break;
111+
return -1;
112112
}
113113
len -= process;
114114
cp += process;
115115
}
116+
return 0;
116117
}
117118

118119
/* Internal methods for a hash object */
@@ -243,6 +244,7 @@ static PyObject *
243244
EVP_update(EVPobject *self, PyObject *obj)
244245
/*[clinic end generated code: output=ec1d55ed2432e966 input=9b30ec848f015501]*/
245246
{
247+
int result;
246248
Py_buffer view;
247249

248250
GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
@@ -255,14 +257,17 @@ EVP_update(EVPobject *self, PyObject *obj)
255257
if (self->lock != NULL) {
256258
Py_BEGIN_ALLOW_THREADS
257259
PyThread_acquire_lock(self->lock, 1);
258-
EVP_hash(self, view.buf, view.len);
260+
result = EVP_hash(self, view.buf, view.len);
259261
PyThread_release_lock(self->lock);
260262
Py_END_ALLOW_THREADS
261263
} else {
262-
EVP_hash(self, view.buf, view.len);
264+
result = EVP_hash(self, view.buf, view.len);
263265
}
264266

265267
PyBuffer_Release(&view);
268+
269+
if (result == -1)
270+
return NULL;
266271
Py_RETURN_NONE;
267272
}
268273

@@ -396,6 +401,7 @@ static PyObject *
396401
EVPnew(const EVP_MD *digest,
397402
const unsigned char *cp, Py_ssize_t len)
398403
{
404+
int result = 0;
399405
EVPobject *self;
400406

401407
if (!digest) {
@@ -415,10 +421,14 @@ EVPnew(const EVP_MD *digest,
415421
if (cp && len) {
416422
if (len >= HASHLIB_GIL_MINSIZE) {
417423
Py_BEGIN_ALLOW_THREADS
418-
EVP_hash(self, cp, len);
424+
result = EVP_hash(self, cp, len);
419425
Py_END_ALLOW_THREADS
420426
} else {
421-
EVP_hash(self, cp, len);
427+
result = EVP_hash(self, cp, len);
428+
}
429+
if (result == -1) {
430+
Py_DECREF(self);
431+
return NULL;
422432
}
423433
}
424434

0 commit comments

Comments
 (0)