Skip to content

Commit

Permalink
Fix resource leak
Browse files Browse the repository at this point in the history
  • Loading branch information
ichizok committed Jul 29, 2018
1 parent 04654b3 commit df04338
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions src/if_py_both.h
Expand Up @@ -550,25 +550,51 @@ typedef struct
{
PyObject_HEAD
char *fullname;
PyObject *target;
PyObject *result;
} LoaderObject;
static PyTypeObject LoaderType;

static void
LoaderDestructor(LoaderObject *self)
{
vim_free(self->fullname);
Py_DECREF(self->target);
Py_XDECREF(self->result);
DESTRUCTOR_FINISH(self);
}

static PyObject *
LoaderLoadModule(LoaderObject *self, PyObject *args UNUSED)
{
char *fullname = self->fullname;
PyObject *target = self->target;
PyObject *result = self->result;
PyObject *module;

if (!fullname)
{
module = result ? result : Py_None;
Py_INCREF(module);
return module;
}

module = call_load_module(fullname, (int)STRLEN(fullname), result);

self->fullname = NULL;
self->result = module;

vim_free(fullname);
Py_DECREF(result);

if (!module)
{
if (PyErr_Occurred())
return NULL;

Py_INCREF(Py_None);
return Py_None;
}

return call_load_module(fullname, (int)STRLEN(fullname), target);
Py_INCREF(module);
return module;
}

static struct PyMethodDef LoaderMethods[] = {
Expand Down Expand Up @@ -1305,7 +1331,7 @@ find_module(char *fullname, char *tail, PyObject *new_path)
FinderFindModule(PyObject *self, PyObject *args)
{
char *fullname;
PyObject *target;
PyObject *result;
PyObject *new_path;
LoaderObject *loader;

Expand All @@ -1315,11 +1341,11 @@ FinderFindModule(PyObject *self, PyObject *args)
if (!(new_path = Vim_GetPaths(self)))
return NULL;

target = find_module(fullname, fullname, new_path);
result = find_module(fullname, fullname, new_path);

Py_DECREF(new_path);

if (!target)
if (!result)
{
if (PyErr_Occurred())
return NULL;
Expand All @@ -1328,14 +1354,22 @@ FinderFindModule(PyObject *self, PyObject *args)
return Py_None;
}

if (!(fullname = (char *)vim_strsave((char_u *)fullname)))
{
Py_DECREF(result);
PyErr_NoMemory();
return NULL;
}

if (!(loader = PyObject_NEW(LoaderObject, &LoaderType)))
{
Py_DECREF(target);
vim_free(fullname);
Py_DECREF(result);
return NULL;
}

loader->fullname = (char *)vim_strsave((char_u *)fullname);
loader->target = target;
loader->fullname = fullname;
loader->result = result;

return (PyObject *) loader;
}
Expand Down

0 comments on commit df04338

Please sign in to comment.