From 78f104f300774f9fd56d306ed8e7dccbec79022a Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Mon, 4 Mar 2019 16:45:41 +0100 Subject: [PATCH] [3.6] bpo-36179: Fix ref leaks in _hashopenssl (GH-12158) Fix two unlikely reference leaks in _hashopenssl. The leaks only occur in out-of-memory cases. Thanks to Charalampos Stratakis. Signed-off-by: Christian Heimes https://bugs.python.org/issue36179. (cherry picked from commit b7bc283ab6a23ee98784400ebffe7fe410232a2e) Co-authored-by: Christian Heimes --- .../2019-03-04-10-42-46.bpo-36179.jEyuI-.rst | 2 ++ Modules/_hashopenssl.c | 14 ++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-03-04-10-42-46.bpo-36179.jEyuI-.rst diff --git a/Misc/NEWS.d/next/Library/2019-03-04-10-42-46.bpo-36179.jEyuI-.rst b/Misc/NEWS.d/next/Library/2019-03-04-10-42-46.bpo-36179.jEyuI-.rst new file mode 100644 index 000000000000000..61a98778b78e9d0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-03-04-10-42-46.bpo-36179.jEyuI-.rst @@ -0,0 +1,2 @@ +Fix two unlikely reference leaks in _hashopenssl. The leaks only occur in +out-of-memory cases. diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 84edd72c85a36d2..a38f74c9c212481 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -113,12 +113,6 @@ newEVPobject(PyObject *name) return NULL; } - retval->ctx = EVP_MD_CTX_new(); - if (retval->ctx == NULL) { - PyErr_NoMemory(); - return NULL; - } - /* save the name for .name to return */ Py_INCREF(name); retval->name = name; @@ -126,6 +120,13 @@ newEVPobject(PyObject *name) retval->lock = NULL; #endif + retval->ctx = EVP_MD_CTX_new(); + if (retval->ctx == NULL) { + Py_DECREF(retval); + PyErr_NoMemory(); + return NULL; + } + return retval; } @@ -186,6 +187,7 @@ EVP_copy(EVPobject *self, PyObject *unused) return NULL; if (!locked_EVP_MD_CTX_copy(newobj->ctx, self)) { + Py_DECREF(newobj); return _setException(PyExc_ValueError); } return (PyObject *)newobj;