- Issue #3745: Fix hashlib to always reject unicode and non buffer-api
supporting objects as input no matter how it was compiled (built in
implementations or external openssl library).
(backported from a py3k branch)
diff --git a/Modules/md5module.c b/Modules/md5module.c
index 1e2e6e3..0b150a0 100644
--- a/Modules/md5module.c
+++ b/Modules/md5module.c
@@ -12,6 +12,7 @@
#include "Python.h"
#include "structmember.h"
#include "md5.h"
+#include "hashlib.h"
typedef struct {
PyObject_HEAD
@@ -50,14 +51,18 @@
static PyObject *
md5_update(md5object *self, PyObject *args)
{
- unsigned char *cp;
- int len;
+ PyObject *data_obj;
+ Py_buffer view;
- if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
+ if (!PyArg_ParseTuple(args, "O:update", &data_obj))
return NULL;
- md5_append(&self->md5, cp, len);
+ GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
+ md5_append(&self->md5, (unsigned char*)view.buf,
+ Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
+
+ PyBuffer_Release(&view);
Py_INCREF(Py_None);
return Py_None;
}
@@ -261,18 +266,25 @@
MD5_new(PyObject *self, PyObject *args)
{
md5object *md5p;
- unsigned char *cp = NULL;
- int len = 0;
+ PyObject *data_obj = NULL;
+ Py_buffer view;
- if (!PyArg_ParseTuple(args, "|s#:new", &cp, &len))
+ if (!PyArg_ParseTuple(args, "|O:new", &data_obj))
return NULL;
- if ((md5p = newmd5object()) == NULL)
+ GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
+
+ if ((md5p = newmd5object()) == NULL) {
+ PyBuffer_Release(&view);
return NULL;
+ }
- if (cp)
- md5_append(&md5p->md5, cp, len);
+ if (data_obj) {
+ md5_append(&md5p->md5, (unsigned char*)view.buf,
+ Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
+ }
+ PyBuffer_Release(&view);
return (PyObject *)md5p;
}