]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[mod_python] Fix PyObject leaks in eval_some_python function
authorsunlin <lin.sun@zoom.us>
Fri, 5 Feb 2021 10:36:45 +0000 (18:36 +0800)
committerGitHub <noreply@github.com>
Fri, 5 Feb 2021 10:36:45 +0000 (13:36 +0300)
src/mod/languages/mod_python/mod_python.c

index c9ae2b895d40014b3969ba1e14f46bc6161665e3..21a62d54fcda74d3a7a89da6129ba190c38c5166 100644 (file)
@@ -177,7 +177,7 @@ static void eval_some_python(const char *funcname, char *args, switch_core_sessi
        char *argv[2] = { 0 };
        int argc;
        char *script = NULL;
-       PyObject *module = NULL, *sp = NULL, *stp = NULL, *eve = NULL;
+       PyObject *module_o = NULL, *module = NULL, *sp = NULL, *stp = NULL, *eve = NULL;
        PyObject *function = NULL;
        PyObject *arg = NULL;
        PyObject *result = NULL;
@@ -248,15 +248,15 @@ static void eval_some_python(const char *funcname, char *args, switch_core_sessi
        }
 
        // import the module
-       module = PyImport_ImportModule((char *) script);
-       if (!module) {
+       module_o = PyImport_ImportModule((char *) script);
+       if (!module_o) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error importing module\n");
                print_python_error(script);
                PyErr_Clear();
                goto done_swap_out;
        }
        // reload the module
-       module = PyImport_ReloadModule(module);
+       module = PyImport_ReloadModule(module_o);
        if (!module) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error reloading module\n");
                print_python_error(script);
@@ -285,6 +285,7 @@ static void eval_some_python(const char *funcname, char *args, switch_core_sessi
        if (stream) {
                stp = mod_python_conjure_stream(stream);
                if (stream->param_event) {
+                       Py_XDECREF(eve);
                        eve = mod_python_conjure_event(stream->param_event);
                }
        }
@@ -304,7 +305,6 @@ static void eval_some_python(const char *funcname, char *args, switch_core_sessi
        // invoke the handler
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Call python script \n");
        result = PyEval_CallObjectWithKeywords(function, arg, (PyObject *) NULL);
-       Py_DECREF(function);
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Finished calling python script \n");
 
        // check the result and print out any errors
@@ -323,13 +323,15 @@ static void eval_some_python(const char *funcname, char *args, switch_core_sessi
 
   done_swap_out:
 
-       if (arg) {
-               Py_DECREF(arg);
-       }
 
-       if (sp) {
-               Py_DECREF(sp);
-       }
+       Py_XDECREF(result);
+       Py_XDECREF(arg);
+       Py_XDECREF(function);
+       Py_XDECREF(module);
+       Py_XDECREF(module_o);
+       Py_XDECREF(stp);
+       Py_XDECREF(eve);
+       Py_XDECREF(sp);
 
        if (tstate) {
                // thread state must be cleared explicitly or we'll get memory leaks
@@ -342,8 +344,6 @@ static void eval_some_python(const char *funcname, char *args, switch_core_sessi
 
        switch_safe_free(dupargs);
        switch_safe_free(script);
-
-
 }