python: Fix memory leak with ParseTuple (using 'es' format)
authorNoel Power <noel.power@suse.com>
Fri, 9 Nov 2018 16:47:00 +0000 (16:47 +0000)
committerDouglas Bagnall <dbagnall@samba.org>
Sat, 12 Jan 2019 23:40:26 +0000 (00:40 +0100)
Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
python/pyglue.c

index 22ac53f..e4f961f 100644 (file)
@@ -300,28 +300,36 @@ static PyObject *py_strcasecmp_m(PyObject *self, PyObject *args)
 {
        const char *s1 = NULL;
        const char *s2 = NULL;
-
+       long cmp_result = 0;
        if (!PyArg_ParseTuple(args, "eses", "utf8", &s1, "utf8", &s2)) {
                return NULL;
        }
 
-       return PyInt_FromLong(strcasecmp_m(s1, s2));
+       cmp_result = strcasecmp_m(s1, s2);
+       PyMem_Free(discard_const_p(char, s1));
+       PyMem_Free(discard_const_p(char, s2));
+       return PyInt_FromLong(cmp_result);
 }
 
 static PyObject *py_strstr_m(PyObject *self, PyObject *args)
 {
        const char *s1 = NULL;
        const char *s2 = NULL;
-       char *ret = NULL;
-
+       char *strstr_ret = NULL;
+       PyObject *result = NULL;
        if (!PyArg_ParseTuple(args, "eses", "utf8", &s1, "utf8", &s2))
                return NULL;
 
-       ret = strstr_m(s1, s2);
-       if (!ret) {
+       strstr_ret = strstr_m(s1, s2);
+       if (!strstr_ret) {
+               PyMem_Free(discard_const_p(char, s1));
+               PyMem_Free(discard_const_p(char, s2));
                Py_RETURN_NONE;
        }
-       return PyUnicode_FromString(ret);
+       result = PyUnicode_FromString(strstr_ret);
+       PyMem_Free(discard_const_p(char, s1));
+       PyMem_Free(discard_const_p(char, s2));
+       return result;
 }
 
 static PyMethodDef py_misc_methods[] = {