Fix SHA_new and MD5_new, that would crash if not given initial data

This commit is contained in:
Kristján Valur Jónsson 2009-03-03 03:20:42 +00:00
parent 7d49bba969
commit 7705d0aaaf
2 changed files with 13 additions and 8 deletions

View File

@ -272,19 +272,21 @@ MD5_new(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "|O:new", &data_obj)) if (!PyArg_ParseTuple(args, "|O:new", &data_obj))
return NULL; return NULL;
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL); if (data_obj)
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
if ((md5p = newmd5object()) == NULL) { if ((md5p = newmd5object()) == NULL) {
PyBuffer_Release(&view); if (data_obj)
PyBuffer_Release(&view);
return NULL; return NULL;
} }
if (data_obj) { if (data_obj) {
md5_append(&md5p->md5, (unsigned char*)view.buf, md5_append(&md5p->md5, (unsigned char*)view.buf,
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
PyBuffer_Release(&view);
} }
PyBuffer_Release(&view);
return (PyObject *)md5p; return (PyObject *)md5p;
} }

View File

@ -548,10 +548,12 @@ SHA_new(PyObject *self, PyObject *args, PyObject *kwdict)
return NULL; return NULL;
} }
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL); if (data_obj)
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
if ((new = newSHAobject()) == NULL) { if ((new = newSHAobject()) == NULL) {
PyBuffer_Release(&view); if (data_obj)
PyBuffer_Release(&view);
return NULL; return NULL;
} }
@ -559,15 +561,16 @@ SHA_new(PyObject *self, PyObject *args, PyObject *kwdict)
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
Py_DECREF(new); Py_DECREF(new);
PyBuffer_Release(&view); if (data_obj)
PyBuffer_Release(&view);
return NULL; return NULL;
} }
if (data_obj) { if (data_obj) {
sha_update(new, (unsigned char*)view.buf, sha_update(new, (unsigned char*)view.buf,
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
PyBuffer_Release(&view);
} }
PyBuffer_Release(&view);
return (PyObject *)new; return (PyObject *)new;
} }