From 7705d0aaafb6b034e9a3582e57f77d8f9cb5aa91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Tue, 3 Mar 2009 03:20:42 +0000 Subject: [PATCH] Fix SHA_new and MD5_new, that would crash if not given initial data --- Modules/md5module.c | 10 ++++++---- Modules/shamodule.c | 11 +++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Modules/md5module.c b/Modules/md5module.c index 0b150a0bd25..7081706ba3c 100644 --- a/Modules/md5module.c +++ b/Modules/md5module.c @@ -272,19 +272,21 @@ MD5_new(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "|O:new", &data_obj)) 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) { - PyBuffer_Release(&view); + if (data_obj) + PyBuffer_Release(&view); return NULL; } if (data_obj) { md5_append(&md5p->md5, (unsigned char*)view.buf, Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); + PyBuffer_Release(&view); } - - PyBuffer_Release(&view); + return (PyObject *)md5p; } diff --git a/Modules/shamodule.c b/Modules/shamodule.c index e89a1eab0e3..6399b752a58 100644 --- a/Modules/shamodule.c +++ b/Modules/shamodule.c @@ -548,10 +548,12 @@ SHA_new(PyObject *self, PyObject *args, PyObject *kwdict) 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) { - PyBuffer_Release(&view); + if (data_obj) + PyBuffer_Release(&view); return NULL; } @@ -559,15 +561,16 @@ SHA_new(PyObject *self, PyObject *args, PyObject *kwdict) if (PyErr_Occurred()) { Py_DECREF(new); - PyBuffer_Release(&view); + if (data_obj) + PyBuffer_Release(&view); return NULL; } if (data_obj) { sha_update(new, (unsigned char*)view.buf, Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); + PyBuffer_Release(&view); } - PyBuffer_Release(&view); return (PyObject *)new; }