summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2017-11-23 01:24:07 +0100
committerMatěj Cepl <mcepl@cepl.eu>2018-02-08 17:46:00 +0100
commit17b716a655ca588dddf6de99d9661b7e21e28d60 (patch)
tree0773c389e798ec8b6e0c57d2d15e966595676da2
parent9cf90aec4f1290d5a37d6af2e04abd03266d0e44 (diff)
downloadm2crypto-17b716a655ca588dddf6de99d9661b7e21e28d60.tar.gz
py3k version leaked memory for PyObject with the file name.
-rw-r--r--SWIG/_bio.i5
-rw-r--r--SWIG/_lib.i36
-rw-r--r--SWIG/_py3k_compat.i5
3 files changed, 27 insertions, 19 deletions
diff --git a/SWIG/_bio.i b/SWIG/_bio.i
index 93ff5ad..44706ed 100644
--- a/SWIG/_bio.i
+++ b/SWIG/_bio.i
@@ -110,7 +110,8 @@ BIO *bio_new_pyfile(PyObject *pyfile, int bio_close) {
if (bio == NULL) {
/* Find out the name of the file so we can have good error
* message. */
- char *name = PyBytes_AsString(PyFile_Name(pyfile));
+ PyObject *pyname = m2_PyFile_Name(pyfile);
+ char *name = PyBytes_AsString(pyname);
if (name == NULL) {
PyErr_Format(_bio_err,
@@ -120,7 +121,7 @@ BIO *bio_new_pyfile(PyObject *pyfile, int bio_close) {
PyErr_Format(_bio_err,
"Opening of the new BIO on file %s failed!", name);
}
- return NULL;
+ Py_DECREF(pyname);
}
return bio;
}
diff --git a/SWIG/_lib.i b/SWIG/_lib.i
index 6378c65..4fa1537 100644
--- a/SWIG/_lib.i
+++ b/SWIG/_lib.i
@@ -145,20 +145,20 @@ static int m2_PyObject_GetBufferInt(PyObject *obj, Py_buffer *view, int flags)
int ret;
if (PyObject_CheckBuffer(obj))
- ret = PyObject_GetBuffer(obj, view, flags);
+ ret = PyObject_GetBuffer(obj, view, flags);
else {
- const void *buf;
+ const void *buf;
- ret = PyObject_AsReadBuffer(obj, &buf, &view->len);
- if (ret == 0)
- view->buf = (void *)buf;
+ ret = PyObject_AsReadBuffer(obj, &buf, &view->len);
+ if (ret == 0)
+ view->buf = (void *)buf;
}
if (ret)
- return ret;
+ return ret;
if (view->len > INT_MAX) {
- PyErr_SetString(PyExc_ValueError, "object too large");
- m2_PyBuffer_Release(obj, view);
- return -1;
+ PyErr_SetString(PyExc_ValueError, "object too large");
+ m2_PyBuffer_Release(obj, view);
+ return -1;
}
return 0;
@@ -185,7 +185,7 @@ m2_PyObject_AsBIGNUM(PyObject* value, PyObject* _py_exc)
static void m2_PyBuffer_Release(PyObject *obj, Py_buffer *view)
{
if (PyObject_CheckBuffer(obj))
- PyBuffer_Release(view);
+ PyBuffer_Release(view);
/* else do nothing, view->buf comes from PyObject_AsReadBuffer */
}
@@ -207,6 +207,18 @@ m2_PyString_AsStringAndSizeInt(PyObject *obj, char **s, int *len)
return 0;
}
+/* Works as PyFile_Name, but always returns a new object. */
+PyObject *m2_PyFile_Name(PyObject *pyfile) {
+ PyObject *out = NULL;
+#if PY_MAJOR_VERSION >= 3
+ out = PyObject_GetAttrString(pyfile, "name");
+#else
+ out = PyFile_Name(pyfile);
+ Py_XINCREF(out);
+#endif
+ return out;
+}
+
#define m2_PyErr_Msg(type) m2_PyErr_Msg_Caller(type, __func__)
static void m2_PyErr_Msg_Caller(PyObject *err_type, const char* caller) {
@@ -267,9 +279,9 @@ int ssl_verify_callback(int ok, X509_STORE_CTX *ctx) {
_x509_store_ctx_swigptr = SWIG_NewPointerObj((void *)ctx, SWIGTYPE_p_X509_STORE_CTX, 0);
_x509_store_ctx_obj = Py_BuildValue("(Oi)", _x509_store_ctx_swigptr, 0);
- _x509_store_ctx_inst = PyObject_CallObject(_klass, _x509_store_ctx_obj);
+ _x509_store_ctx_inst = PyObject_CallObject(_klass, _x509_store_ctx_obj);
- argv = Py_BuildValue("(iO)", ok, _x509_store_ctx_inst);
+ argv = Py_BuildValue("(iO)", ok, _x509_store_ctx_inst);
} else {
if (PyErr_Warn(PyExc_DeprecationWarning, "Old style callback, use cb_func(ok, store) instead")) {
warning_raised_exception = 1;
diff --git a/SWIG/_py3k_compat.i b/SWIG/_py3k_compat.i
index e694463..6cb595e 100644
--- a/SWIG/_py3k_compat.i
+++ b/SWIG/_py3k_compat.i
@@ -1,11 +1,6 @@
%{
#if PY_MAJOR_VERSION >= 3
-/* Return the name of the file specified by p as a string object. */
-PyObject* PyFile_Name(PyObject *pyfile) {
- return PyObject_GetAttrString(pyfile, "name");
-}
-
FILE* PyFile_AsFile(PyObject *pyfile) {
FILE* fp;
int fd;