Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
diff --git a/Modules/shamodule.c b/Modules/shamodule.c
index df73441..656208d 100644
--- a/Modules/shamodule.c
+++ b/Modules/shamodule.c
@@ -429,12 +429,25 @@
SHA_update(SHAobject *self, PyObject *args)
{
Py_buffer view;
+ Py_ssize_t n;
+ unsigned char *buf;
if (!PyArg_ParseTuple(args, "s*:update", &view))
return NULL;
- sha_update(self, (unsigned char*)view.buf,
- Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
+ n = view.len;
+ buf = (unsigned char *) view.buf;
+ while (n > 0) {
+ Py_ssize_t nbytes;
+ if (n > INT_MAX)
+ nbytes = INT_MAX;
+ else
+ nbytes = n;
+ sha_update(self, buf,
+ Py_SAFE_DOWNCAST(nbytes, Py_ssize_t, unsigned int));
+ buf += nbytes;
+ n -= nbytes;
+ }
PyBuffer_Release(&view);
Py_RETURN_NONE;