Skip to content

Commit

Permalink
app_python: several free of allocated vars in case of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
miconda committed Jul 15, 2017
1 parent 15de1f0 commit 8353b03
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
57 changes: 47 additions & 10 deletions src/modules/app_python/app_python_mod.c
Expand Up @@ -109,19 +109,25 @@ static int mod_init(void)
if(dname_src==NULL || bname_src==NULL)
{
LM_ERR("no more pkg memory\n");
if(dname_src) pkg_free(dname_src);
if(bname_src) pkg_free(bname_src);
return -1;
}

dname = strdup(dirname(dname_src));
if(dname==NULL) {
LM_ERR("no more system memory\n");
pkg_free(dname_src);
pkg_free(bname_src);
return -1;
}
if (strlen(dname) == 0) {
free(dname);
dname = malloc(2);
if(dname==NULL) {
LM_ERR("no more system memory\n");
pkg_free(dname_src);
pkg_free(bname_src);
return -1;
}
dname[0] = '.';
Expand All @@ -136,6 +142,8 @@ static int mod_init(void)
} else {
LM_ERR("%s: script_name doesn't look like a python script\n",
script_name.s);
pkg_free(dname_src);
pkg_free(bname_src);
return -1;
}

Expand All @@ -149,27 +157,35 @@ static int mod_init(void)
{
Py_XDECREF(format_exc_obj);
PyEval_ReleaseLock();
pkg_free(dname_src);
pkg_free(bname_src);
return -1;
}

sys_path = PySys_GetObject("path");
/* PySys_GetObject doesn't pass reference! No need to DEREF */
if (sys_path == NULL) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_AttributeError, "'module' object 'sys' has no attribute 'path'");
PyErr_Format(PyExc_AttributeError,
"'module' object 'sys' has no attribute 'path'");
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
PyEval_ReleaseLock();
pkg_free(dname_src);
pkg_free(bname_src);
return -1;
}

pDir = PyString_FromString(dname);
if (pDir == NULL) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_AttributeError, "PyString_FromString() has failed");
PyErr_Format(PyExc_AttributeError,
"PyString_FromString() has failed");
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
PyEval_ReleaseLock();
pkg_free(dname_src);
pkg_free(bname_src);
return -1;
}

Expand All @@ -182,15 +198,20 @@ static int mod_init(void)
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
PyEval_ReleaseLock();
pkg_free(dname_src);
pkg_free(bname_src);
return -1;
}

if (python_msgobj_init() != 0) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_AttributeError, "python_msgobj_init() has failed");
PyErr_SetString(PyExc_AttributeError,
"python_msgobj_init() has failed");
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
PyEval_ReleaseLock();
pkg_free(dname_src);
pkg_free(bname_src);
return -1;
}

Expand All @@ -201,6 +222,8 @@ static int mod_init(void)
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
PyEval_ReleaseLock();
pkg_free(dname_src);
pkg_free(bname_src);
return -1;
}

Expand All @@ -214,7 +237,9 @@ static int mod_init(void)

if (pFunc == NULL) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_AttributeError, "'module' object '%s' has no attribute '%s'", bname, mod_init_fname.s);
PyErr_Format(PyExc_AttributeError,
"'module' object '%s' has no attribute '%s'",
bname, mod_init_fname.s);
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pFunc);
Expand All @@ -224,7 +249,9 @@ static int mod_init(void)

if (!PyCallable_Check(pFunc)) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_AttributeError, "module object '%s' has is not callable attribute '%s'", bname, mod_init_fname.s);
PyErr_Format(PyExc_AttributeError,
"module object '%s' has is not callable attribute '%s'",
bname, mod_init_fname.s);
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pFunc);
Expand All @@ -249,7 +276,9 @@ static int mod_init(void)

if (_sr_apy_handler_obj == Py_None) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned None. Should be a class instance.", mod_init_fname.s, bname);
PyErr_Format(PyExc_TypeError,
"Function '%s' of module '%s' has returned None."
" Should be a class instance.", mod_init_fname.s, bname);
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
PyEval_ReleaseLock();
Expand All @@ -267,7 +296,10 @@ static int mod_init(void)
if (_sr_apy_handler_obj == NULL) {
LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
if (!PyErr_Occurred())
PyErr_Format(PyExc_TypeError, "Function '%s' of module '%s' has returned not returned object. Should be a class instance.", mod_init_fname.s, bname);
PyErr_Format(PyExc_TypeError,
"Function '%s' of module '%s' has returned not returned"
" object. Should be a class instance.",
mod_init_fname.s, bname);
python_handle_exception("mod_init");
Py_DECREF(format_exc_obj);
PyEval_ReleaseLock();
Expand Down Expand Up @@ -297,7 +329,8 @@ static int child_init(int rank)
if (classname == NULL)
{
if (!PyErr_Occurred())
PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
PyErr_Format(PyExc_AttributeError,
"'module' instance has no class name");
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
PyThreadState_Swap(NULL);
Expand All @@ -318,7 +351,9 @@ static int child_init(int rank)

if (!PyCallable_Check(pFunc)) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
PyErr_Format(PyExc_AttributeError,
"class object '%s' has is not callable attribute '%s'",
!classname ? "None" : classname, mod_init_fname.s);
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pFunc);
Expand Down Expand Up @@ -376,7 +411,9 @@ static int child_init(int rank)
if (!PyInt_Check(pResult))
{
if (!PyErr_Occurred())
PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
PyErr_Format(PyExc_TypeError,
"method '%s' of class '%s' should return 'int' type",
child_init_mname.s, !classname ? "None" : classname);
python_handle_exception("child_init");
Py_DECREF(format_exc_obj);
Py_XDECREF(pResult);
Expand Down
7 changes: 6 additions & 1 deletion src/modules/app_python/python_msgobj.c
Expand Up @@ -143,7 +143,9 @@ static PyObject *msg_getHeader(msgobject *self, PyObject *args)
return NULL;
hname.len = strlen(hname.s);

parse_headers(self->msg, ~0, 0);
if(parse_headers(self->msg, HDR_EOH_F, 0)<0) {
LM_ERR("failed to parse msg headers\n");
}
hbody = NULL;
for (hf = self->msg->headers; hf != NULL; hf = hf->next) {
if (hname.len == hf->name.len &&
Expand Down Expand Up @@ -214,6 +216,7 @@ PyObject *msg_call_function(msgobject *self, PyObject *args)
if (rval < 0) {
PyErr_SetString(PyExc_RuntimeError, "Error in fixup (2)");
Py_INCREF(Py_None);
pkg_free(act);
return Py_None;
}
act->val[3].type = MODFIXUP_ST;
Expand All @@ -223,6 +226,7 @@ PyObject *msg_call_function(msgobject *self, PyObject *args)
if (rval < 0) {
PyErr_SetString(PyExc_RuntimeError, "Error in fixup (1)");
Py_INCREF(Py_None);
pkg_free(act);
return Py_None;
}
act->val[2].type = MODFIXUP_ST;
Expand All @@ -232,6 +236,7 @@ PyObject *msg_call_function(msgobject *self, PyObject *args)
if (rval < 0) {
PyErr_SetString(PyExc_RuntimeError, "Error in fixup (0)");
Py_INCREF(Py_None);
pkg_free(act);
return Py_None;
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/modules/app_python/python_support.c
Expand Up @@ -60,6 +60,7 @@ void python_handle_exception(const char *fmt, ...)
PyErr_NormalizeException(&exception, &v, &tb);
if (exception == NULL) {
LM_ERR("Can't get traceback, PyErr_NormalizeException() has failed.\n");
if (buf) pkg_free(srcbuf);
return;
}

Expand All @@ -70,13 +71,15 @@ void python_handle_exception(const char *fmt, ...)
Py_XDECREF(tb);
if (args == NULL) {
LM_ERR("Can't get traceback, PyTuple_Pack() has failed.\n");
if (buf) pkg_free(srcbuf);
return;
}

pResult = PyObject_CallObject(format_exc_obj, args);
Py_DECREF(args);
if (pResult == NULL) {
LM_ERR("Can't get traceback, traceback.format_exception() has failed.\n");
if (buf) pkg_free(srcbuf);
return;
}

Expand All @@ -86,6 +89,7 @@ void python_handle_exception(const char *fmt, ...)
{
LM_ERR("Can't allocate memory (%lu bytes), pkg_realloc() has failed."
" Not enough memory.\n", (unsigned long)(buflen * sizeof(char *)));
if (buf) pkg_free(srcbuf);
return;
}
memset(buf, 0, buflen * sizeof(char));
Expand All @@ -97,6 +101,7 @@ void python_handle_exception(const char *fmt, ...)
Py_DECREF(pResult);
if (buf)
pkg_free(buf);
if (buf) pkg_free(srcbuf);
return;
}

Expand All @@ -108,6 +113,7 @@ void python_handle_exception(const char *fmt, ...)
Py_DECREF(pResult);
if (buf)
pkg_free(buf);
if (buf) pkg_free(srcbuf);
return;
}

Expand All @@ -121,6 +127,7 @@ void python_handle_exception(const char *fmt, ...)
" Not enough memory.\n", (unsigned long)(buflen * sizeof(char *)));
Py_DECREF(line);
Py_DECREF(pResult);
if (buf) pkg_free(srcbuf);
return;
}

Expand Down

0 comments on commit 8353b03

Please sign in to comment.