fixing several memory leaks in pygit2

* fixed several ref counter issues
* us Py_CLEAR through the whole lib
* refactorization
  * Config does not need Repository pointer
    -> No need for cyclic garbage collecter support
This commit is contained in:
Nico von Geyso
2013-03-10 14:10:22 +01:00
parent 8b2abc1ca7
commit 8cb0539858
13 changed files with 151 additions and 118 deletions

View File

@@ -32,6 +32,7 @@
#include <Python.h>
#include <git2.h>
PyObject* wrap_config(char *c_path);
PyObject* Config_get_global_config(void);
PyObject* Config_get_system_config(void);
PyObject* Config_add_file(Config *self, PyObject *args, PyObject *kwds);
@@ -39,6 +40,6 @@ PyObject* Config_getitem(Config *self, PyObject *key);
PyObject* Config_foreach(Config *self, PyObject *args);
PyObject* Config_get_multivar(Config *self, PyObject *args);
PyObject* Config_set_multivar(Config *self, PyObject *args);
int Config_init(Config *self, PyObject *args, PyObject *kwds);
int Config_setitem(Config *self, PyObject *key, PyObject *value);
#endif

View File

@@ -57,10 +57,14 @@ OBJECT_STRUCT(Blob, git_blob, blob)
OBJECT_STRUCT(Tag, git_tag, tag)
OBJECT_STRUCT(Index, git_index, index)
OBJECT_STRUCT(Walker, git_revwalk, walk)
OBJECT_STRUCT(Config, git_config, config)
OBJECT_STRUCT(Remote, git_remote, remote)
OBJECT_STRUCT(Diff, git_diff_list, list)
typedef struct {
PyObject_HEAD
git_config* config;
} Config;
typedef struct {
PyObject_HEAD
git_diff_list* list;
@@ -128,7 +132,6 @@ typedef struct {
typedef struct {
PyObject_HEAD
Reference *reference;
git_reflog *reflog;
int i;
int size;

View File

@@ -95,9 +95,6 @@ char * py_str_to_c_str(PyObject *value, const char *encoding);
#define py_path_to_c_str(py_path) \
py_str_to_c_str(py_path, Py_FileSystemDefaultEncoding)
#define INSTANCIATE_CLASS(type, arglist) \
PyObject_CallObject(PyType_GenericNew(&type, NULL, NULL), arglist);
/* Helpers to make shorter PyMethodDef and PyGetSetDef blocks */
#define METHOD(type, name, args)\
{#name, (PyCFunction) type ## _ ## name, args, type ## _ ## name ## __doc__}
@@ -119,21 +116,22 @@ char * py_str_to_c_str(PyObject *value, const char *encoding);
#define MEMBER(type, attr, attr_type, docstr)\
{#attr, attr_type, offsetof(type, attr), 0, PyDoc_STR(docstr)}
/* Helpers for memory allocation */
#define MALLOC(ptr, size, label) \
ptr = realloc(ptr, size * sizeof(char));\
#define CALLOC(ptr, num, size, label) \
ptr = calloc((num), size);\
if (ptr == NULL) {\
err = GIT_ERROR;\
giterr_set_oom();\
goto label;\
}\
#define FREE(to_free)\
if (to_free != NULL) { free(to_free); to_free = NULL; }
#define FREE_FUNC(to_free, fnct)\
if (to_free != NULL) { fnct(to_free); to_free = NULL; }
}
#define MALLOC(ptr, size, label) \
ptr = malloc(size);\
if (ptr == NULL) {\
err = GIT_ERROR;\
giterr_set_oom();\
goto label;\
}
#endif

View File

@@ -34,10 +34,28 @@
extern PyTypeObject ConfigType;
PyObject *
wrap_config(char *c_path) {
int err;
PyObject *py_path;
Config *py_config;
py_path = Py_BuildValue("(s)", c_path);
py_config = PyObject_New(Config, &ConfigType);
err = Config_init(py_config, py_path, NULL);
if (err < 0)
return NULL;
return (PyObject*) py_config;
}
int
Config_init(Config *self, PyObject *args, PyObject *kwds)
{
char *path;
char *path = NULL;
int err;
if (kwds) {
@@ -46,18 +64,17 @@ Config_init(Config *self, PyObject *args, PyObject *kwds)
return -1;
}
if (PySequence_Length(args) > 0) {
if (!PyArg_ParseTuple(args, "s", &path)) {
return -1;
}
if (!PyArg_ParseTuple(args, "|s", &path))
return -1;
if (path == NULL)
err = git_config_new(&self->config);
else
err = git_config_open_ondisk(&self->config, path);
} else {
err = git_config_new(&self->config);
}
if (err < 0) {
git_config_free(self->config);
if (err == GIT_ENOTFOUND) {
Error_set_exc(PyExc_IOError);
} else {
@@ -70,35 +87,14 @@ Config_init(Config *self, PyObject *args, PyObject *kwds)
return 0;
}
void
Config_dealloc(Config *self)
{
PyObject_GC_UnTrack(self);
Py_XDECREF(self->repo);
git_config_free(self->config);
PyObject_GC_Del(self);
PyObject_Del(self);
}
int
Config_traverse(Config *self, visitproc visit, void *arg)
{
Py_VISIT(self->repo);
return 0;
}
PyObject *
Config_open(char *c_path) {
PyObject *py_path = Py_BuildValue("(s)", c_path);
Config *config = PyObject_GC_New(Config, &ConfigType);
Config_init(config, py_path, NULL);
Py_INCREF(config);
return (PyObject *)config;
}
PyDoc_STRVAR(Config_get_global_config__doc__,
"get_global_config() -> Config\n"
"\n"
@@ -116,10 +112,11 @@ Config_get_global_config(void)
PyErr_SetString(PyExc_IOError, "Global config file not found.");
return NULL;
}
return Error_set(err);
}
return Config_open(path);
return wrap_config(path);
}
@@ -143,9 +140,10 @@ Config_get_system_config(void)
return Error_set(err);
}
return Config_open(path);
return wrap_config(path);
}
int
Config_contains(Config *self, PyObject *py_key) {
int err;
@@ -158,9 +156,11 @@ Config_contains(Config *self, PyObject *py_key) {
err = git_config_get_string(&c_value, self->config, c_key);
free(c_key);
if (err == GIT_ENOTFOUND)
return 0;
if (err < 0) {
if (err == GIT_ENOTFOUND)
return 0;
Error_set(err);
return -1;
}
@@ -168,69 +168,71 @@ Config_contains(Config *self, PyObject *py_key) {
return 1;
}
PyObject *
Config_getitem(Config *self, PyObject *py_key)
{
int err;
int64_t c_intvalue;
int c_boolvalue;
const char *c_charvalue;
char *c_key;
long value_int;
int err, value_bool;
const char *value_str;
char *key;
PyObject* py_value;
if (!(c_key = py_str_to_c_str(py_key, NULL)))
key = py_str_to_c_str(py_key, NULL);
if (key == NULL)
return NULL;
err = git_config_get_int64(&c_intvalue, self->config, c_key);
if (err == GIT_OK) {
free(c_key);
return PyInt_FromLong((long)c_intvalue);
}
err = git_config_get_string(&value_str, self->config, key);
if (err < 0)
goto cleanup;
err = git_config_get_bool(&c_boolvalue, self->config, c_key);
if (err == GIT_OK) {
free(c_key);
return PyBool_FromLong((long)c_boolvalue);
}
if (git_config_parse_int64(&value_int, value_str) == 0)
py_value = PyLong_FromLong(value_int);
else if(git_config_parse_bool(&value_bool, value_str) == 0)
py_value = PyBool_FromLong(value_bool);
else
py_value = PyUnicode_FromString(value_str);
cleanup:
free(key);
err = git_config_get_string(&c_charvalue, self->config, c_key);
free(c_key);
if (err < 0) {
if (err == GIT_ENOTFOUND) {
PyErr_SetObject(PyExc_KeyError, py_key);
return NULL;
}
return Error_set(err);
}
return PyUnicode_FromString(c_charvalue);
return py_value;
}
int
Config_setitem(Config *self, PyObject *py_key, PyObject *py_value)
{
int err;
char *c_key;
char *py_str;
char *key, *value;
if (!(c_key = py_str_to_c_str(py_key, NULL)))
key = py_str_to_c_str(py_key, NULL);
if (key == NULL)
return -1;
if (!py_value) {
err = git_config_delete_entry(self->config, c_key);
} else if (PyBool_Check(py_value)) {
err = git_config_set_bool(self->config, c_key,
if (py_value == NULL)
err = git_config_delete_entry(self->config, key);
else if (PyBool_Check(py_value)) {
err = git_config_set_bool(self->config, key,
(int)PyObject_IsTrue(py_value));
} else if (PyInt_Check(py_value)) {
err = git_config_set_int64(self->config, c_key,
err = git_config_set_int64(self->config, key,
(int64_t)PyInt_AsLong(py_value));
} else {
py_value = PyObject_Str(py_value);
py_str = py_str_to_c_str(py_value, NULL);
err = git_config_set_string(self->config, c_key, py_str);
free(py_str);
value = py_str_to_c_str(py_value, NULL);
err = git_config_set_string(self->config, key, value);
free(value);
}
free(c_key);
free(key);
if (err < 0) {
Error_set(err);
return -1;
@@ -263,6 +265,8 @@ Config_foreach_callback_wrapper(const git_config_entry *entry, void *c_payload)
if ((c_result = PyLong_AsLong(py_result) == -1))
return -1;
Py_CLEAR(args);
return c_result;
}
@@ -282,7 +286,7 @@ Config_foreach(Config *self, PyObject *args)
{
int ret;
PyObject *py_callback;
PyObject *py_payload;
PyObject *py_payload = NULL;
if (!PyArg_ParseTuple(args, "O|O", &py_callback, &py_payload))
return NULL;
@@ -338,13 +342,14 @@ PyDoc_STRVAR(Config_get_multivar__doc__,
int
Config_get_multivar_fn_wrapper(const git_config_entry *value, void *data)
{
PyObject *list = (PyObject *)data;
PyObject *item = NULL;
if (!(item = PyUnicode_FromString(value->value)))
return -2;
PyList_Append(list, item);
PyList_Append((PyObject *)data, item);
Py_CLEAR(item);
return 0;
}
@@ -353,17 +358,20 @@ PyObject *
Config_get_multivar(Config *self, PyObject *args)
{
int err;
PyObject *list = PyList_New(0);
PyObject *list;
const char *name = NULL;
const char *regex = NULL;
if (!PyArg_ParseTuple(args, "s|s", &name, &regex))
return NULL;
list = PyList_New(0);
err = git_config_get_multivar(self->config, name, regex,
Config_get_multivar_fn_wrapper,
(void *)list);
if (err < 0) {
Py_CLEAR(list);
if (err == GIT_ENOTFOUND)
Error_set(err);
else
@@ -454,9 +462,9 @@ PyTypeObject ConfigType = {
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Py_TPFLAGS_DEFAULT, /* tp_flags */
Config__doc__, /* tp_doc */
(traverseproc)Config_traverse, /* tp_traverse */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */

View File

@@ -43,8 +43,8 @@ extern PyTypeObject TagType;
void
Object_dealloc(Object* self)
{
Py_CLEAR(self->repo);
git_object_free(self->obj);
Py_XDECREF(self->repo);
PyObject_Del(self);
}

View File

@@ -43,9 +43,8 @@ extern PyTypeObject RefLogEntryType;
void RefLogIter_dealloc(RefLogIter *self)
{
Py_XDECREF(self->reference);
git_reflog_free(self->reflog);
PyObject_GC_Del(self);
PyObject_Del(self);
}
PyObject* RefLogIter_iternext(PyObject *self)
@@ -146,7 +145,9 @@ Reference_delete(Reference *self, PyObject *args)
if (err < 0)
return Error_set(err);
git_reference_free(self->reference);
self->reference = NULL; /* Invalidate the pointer */
Py_RETURN_NONE;
}
@@ -172,6 +173,7 @@ Reference_rename(Reference *self, PyObject *py_name)
/* Rename */
err = git_reference_rename(&new_reference, self->reference, c_name, 0);
git_reference_free(self->reference);
free(c_name);
if (err < 0)
return Error_set(err);
@@ -255,6 +257,7 @@ Reference_target__set__(Reference *self, PyObject *py_name)
return -1;
}
git_reference_free(self->reference);
self->reference = new_ref;
return 0;
}
@@ -316,6 +319,7 @@ Reference_oid__set__(Reference *self, PyObject *py_hex)
return -1;
}
git_reference_free(self->reference);
self->reference = new_ref;
return 0;
}
@@ -371,14 +375,10 @@ Reference_log(Reference *self)
CHECK_REFERENCE(self);
iter = PyObject_New(RefLogIter, &RefLogIterType);
if (iter) {
iter->reference = self;
if (iter != NULL) {
git_reflog_read(&iter->reflog, self->reference);
iter->size = git_reflog_entrycount(iter->reflog);
iter->i = 0;
Py_INCREF(self);
Py_INCREF(iter);
}
return (PyObject*)iter;
}
@@ -398,9 +398,9 @@ RefLogEntry_init(RefLogEntry *self, PyObject *args, PyObject *kwds)
static void
RefLogEntry_dealloc(RefLogEntry *self)
{
Py_XDECREF(self->oid_old);
Py_XDECREF(self->oid_new);
Py_XDECREF(self->committer);
Py_CLEAR(self->oid_old);
Py_CLEAR(self->oid_new);
Py_CLEAR(self->committer);
free(self->message);
PyObject_Del(self);
}

View File

@@ -47,6 +47,7 @@ Remote_init(Remote *self, PyObject *args, PyObject *kwds)
return NULL;
self->repo = py_repo;
Py_INCREF(self->repo);
err = git_remote_load(&self->remote, py_repo->repo, name);
if (err < 0)
@@ -59,6 +60,7 @@ Remote_init(Remote *self, PyObject *args, PyObject *kwds)
static void
Remote_dealloc(Remote *self)
{
Py_CLEAR(self->repo);
git_remote_free(self->remote);
PyObject_Del(self);
}
@@ -81,6 +83,7 @@ Remote_name__set__(Remote *self, PyObject* py_name)
name = py_str_to_c_str(py_name, NULL);
if (name != NULL) {
err = git_remote_rename(self->remote, name, NULL, NULL);
free(name);
if (err == GIT_OK)
return 0;
@@ -105,11 +108,12 @@ int
Remote_url__set__(Remote *self, PyObject* py_url)
{
int err;
char* url;
char* url = NULL;
url = py_str_to_c_str(py_url, NULL);
if (url != NULL) {
err = git_remote_set_url(self->remote, url);
free(url);
if (err == GIT_OK)
return 0;

View File

@@ -102,6 +102,9 @@ Repository_init(Repository *self, PyObject *args, PyObject *kwds)
return -1;
}
self->config = NULL;
self->index = NULL;
return 0;
}
@@ -109,7 +112,8 @@ void
Repository_dealloc(Repository *self)
{
PyObject_GC_UnTrack(self);
Py_XDECREF(self->index);
Py_CLEAR(self->index);
Py_CLEAR(self->config);
git_repository_free(self->repo);
PyObject_GC_Del(self);
}
@@ -494,7 +498,7 @@ PyDoc_STRVAR(Repository_config__doc__,
"(if they are available).");
PyObject *
Repository_config__get__(Repository *self, void *closure)
Repository_config__get__(Repository *self)
{
int err;
git_config *config;
@@ -507,20 +511,18 @@ Repository_config__get__(Repository *self, void *closure)
if (err < 0)
return Error_set(err);
py_config = PyObject_GC_New(Config, &ConfigType);
if (!py_config) {
py_config = PyObject_New(Config, &ConfigType);
if (py_config == NULL) {
git_config_free(config);
return NULL;
}
Py_INCREF(self);
py_config->repo = self;
py_config->config = config;
PyObject_GC_Track(py_config);
self->config = (PyObject*)py_config;
} else {
Py_INCREF(self->config);
}
Py_INCREF(self->config);
return self->config;
}
@@ -815,6 +817,7 @@ Repository_lookup_reference(Repository *self, PyObject *py_name)
return err_obj;
}
free(c_name);
/* 3- Make an instance of Reference and return it */
return wrap_reference(c_reference);
}
@@ -888,13 +891,19 @@ Repository_create_symbolic_reference(Repository *self, PyObject *args,
#if PY_MAJOR_VERSION == 2
c_target = PyString_AsString(py_obj);
#else
c_target = PyString_AsString(PyUnicode_AsASCIIString(py_obj));
// increases ref counter, so we have to release it afterwards
PyObject* py_str = PyUnicode_AsASCIIString(py_obj);
c_target = PyString_AsString(py_str);
#endif
if (c_target == NULL)
return NULL;
err = git_reference_symbolic_create(&c_reference, self->repo, c_name,
c_target, force);
#if PY_MAJOR_VERSION > 2
Py_CLEAR(py_str);
#endif
if (err < 0)
return Error_set(err);
@@ -914,9 +923,14 @@ read_status_cb(const char *path, unsigned int status_flags, void *payload)
/* This is the callback that will be called in git_status_foreach. It
* will be called for every path.*/
PyObject *flags;
int err;
flags = PyInt_FromLong((long) status_flags);
PyDict_SetItemString(payload, path, flags);
err = PyDict_SetItemString(payload, path, flags);
Py_CLEAR(flags);
if (err < 0)
return GIT_ERROR;
return GIT_OK;
}
@@ -1103,6 +1117,7 @@ Repository_checkout(Repository *self, PyObject *args, PyObject *kw)
err = git_repository_set_head(self->repo,
git_reference_name(ref->reference));
}
git_object_free(object);
}
} else { /* checkout from head / index */
opts.checkout_strategy = strategy;

View File

@@ -82,12 +82,13 @@ void
Signature_dealloc(Signature *self)
{
if (self->obj)
Py_DECREF(self->obj);
Py_CLEAR(self->obj);
else {
git_signature_free((git_signature*)self->signature);
free((char*)self->encoding);
}
Py_TYPE(self)->tp_free((PyObject*)self);
PyObject_Del(self);
}
@@ -221,6 +222,7 @@ build_signature(Object *obj, const git_signature *signature,
Signature *py_signature;
py_signature = PyObject_New(Signature, &SignatureType);
if (py_signature) {
Py_INCREF(obj);
py_signature->obj = obj;

View File

@@ -42,7 +42,7 @@ extern PyTypeObject IndexType;
void
TreeEntry_dealloc(TreeEntry *self)
{
Py_XDECREF(self->owner);
Py_CLEAR(self->owner);
git_tree_entry_free((git_tree_entry*)self->entry);
PyObject_Del(self);
}
@@ -339,6 +339,7 @@ Tree_diff(Tree *self, PyObject *args)
PyErr_SetObject(PyExc_TypeError, py_obj);
return NULL;
}
if (err < 0)
return Error_set(err);
@@ -352,6 +353,7 @@ Tree_diff(Tree *self, PyObject *args)
return (PyObject*)py_diff;
}
PySequenceMethods Tree_as_sequence = {
0, /* sq_length */
0, /* sq_concat */

View File

@@ -37,7 +37,7 @@
void
TreeBuilder_dealloc(TreeBuilder *self)
{
Py_XDECREF(self->repo);
Py_CLEAR(self->repo);
git_treebuilder_free(self->bld);
PyObject_Del(self);
}

View File

@@ -36,13 +36,13 @@ extern PyTypeObject ReferenceType;
* the string contained in the value argument. */
char * py_str_to_c_str(PyObject *value, const char *encoding)
{
char *c_str = NULL;
/* Case 1: byte string */
if (PyString_Check(value))
return strdup(PyString_AsString(value));
/* Case 2: text string */
if (PyUnicode_Check(value)) {
char *c_str = NULL;
if (encoding == NULL)
value = PyUnicode_AsUTF8String(value);

View File

@@ -38,8 +38,8 @@ extern PyTypeObject CommitType;
void
Walker_dealloc(Walker *self)
{
Py_CLEAR(self->repo);
git_revwalk_free(self->walk);
Py_DECREF(self->repo);
PyObject_Del(self);
}