From e1da538180881c6dcc1d686e504128327a1f56b9 Mon Sep 17 00:00:00 2001 From: Sven Trenkel Date: Sun, 14 Oct 2012 15:12:45 +0000 Subject: [PATCH] python: Fix memory leaks. --- src/python.c | 67 ++++++++++++++---------- src/pyvalues.c | 161 ++++++++++++++++++++++++++++++--------------------------- 2 files changed, 125 insertions(+), 103 deletions(-) diff --git a/src/python.c b/src/python.c index cfc07f61..22f3db64 100644 --- a/src/python.c +++ b/src/python.c @@ -282,17 +282,13 @@ void cpy_log_exception(const char *context) { Py_END_ALLOW_THREADS Py_XDECREF(tn); Py_XDECREF(m); - if (!cpy_format_exception) { + if (!cpy_format_exception || !traceback) { PyErr_Clear(); - Py_XDECREF(type); + Py_DECREF(type); Py_XDECREF(value); Py_XDECREF(traceback); return; } - if (!traceback) { - PyErr_Clear(); - return; - } list = PyObject_CallFunction(cpy_format_exception, "NNN", type, value, traceback); /* New reference. */ if (list) l = PyObject_Length(list); @@ -313,6 +309,9 @@ void cpy_log_exception(const char *context) { } Py_XDECREF(list); PyErr_Clear(); + Py_DECREF(type); + Py_XDECREF(value); + Py_XDECREF(traceback); } static int cpy_read_callback(user_data_t *data) { @@ -335,7 +334,7 @@ static int cpy_read_callback(user_data_t *data) { static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_list, user_data_t *data) { int i; cpy_callback_t *c = data->data; - PyObject *ret, *list, *temp, *dict = NULL, *val; + PyObject *ret, *list, *temp, *dict = NULL; Values *v; CPY_LOCK_THREADS @@ -375,7 +374,7 @@ static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_li CPY_RETURN_FROM_THREADS 0; } } - dict = PyDict_New(); + dict = PyDict_New(); /* New reference. */ if (value_list->meta) { int i, num; char **table; @@ -394,26 +393,26 @@ static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_li if (type == MD_TYPE_STRING) { if (meta_data_get_string(meta, table[i], &string)) continue; - temp = cpy_string_to_unicode_or_bytes(string); + temp = cpy_string_to_unicode_or_bytes(string); /* New reference. */ free(string); PyDict_SetItemString(dict, table[i], temp); Py_XDECREF(temp); } else if (type == MD_TYPE_SIGNED_INT) { if (meta_data_get_signed_int(meta, table[i], &si)) continue; - temp = PyObject_CallFunctionObjArgs((void *) &SignedType, PyLong_FromLongLong(si), (void *) 0); + temp = PyObject_CallFunctionObjArgs((void *) &SignedType, PyLong_FromLongLong(si), (void *) 0); /* New reference. */ PyDict_SetItemString(dict, table[i], temp); Py_XDECREF(temp); } else if (type == MD_TYPE_UNSIGNED_INT) { if (meta_data_get_unsigned_int(meta, table[i], &ui)) continue; - temp = PyObject_CallFunctionObjArgs((void *) &UnsignedType, PyLong_FromUnsignedLongLong(ui), (void *) 0); + temp = PyObject_CallFunctionObjArgs((void *) &UnsignedType, PyLong_FromUnsignedLongLong(ui), (void *) 0); /* New reference. */ PyDict_SetItemString(dict, table[i], temp); Py_XDECREF(temp); } else if (type == MD_TYPE_DOUBLE) { if (meta_data_get_double(meta, table[i], &d)) continue; - temp = PyFloat_FromDouble(d); + temp = PyFloat_FromDouble(d); /* New reference. */ PyDict_SetItemString(dict, table[i], temp); Py_XDECREF(temp); } else if (type == MD_TYPE_BOOLEAN) { @@ -428,8 +427,7 @@ static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_li } free(table); } - val = Values_New(); /* New reference. */ - v = (Values *) val; + v = (Values *) Values_New(); /* New reference. */ sstrncpy(v->data.host, value_list->host, sizeof(v->data.host)); sstrncpy(v->data.type, value_list->type, sizeof(v->data.type)); sstrncpy(v->data.type_instance, value_list->type_instance, sizeof(v->data.type_instance)); @@ -440,9 +438,9 @@ static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_li Py_CLEAR(v->values); v->values = list; Py_CLEAR(v->meta); - v->meta = dict; + v->meta = dict; /* Steals a reference. */ ret = PyObject_CallFunctionObjArgs(c->callback, v, c->data, (void *) 0); /* New reference. */ - Py_XDECREF(val); + Py_XDECREF(v); if (ret == NULL) { cpy_log_exception("write callback"); } else { @@ -484,11 +482,11 @@ static void cpy_log_callback(int severity, const char *message, user_data_t *dat PyObject *ret, *text; CPY_LOCK_THREADS - text = cpy_string_to_unicode_or_bytes(message); + text = cpy_string_to_unicode_or_bytes(message); /* New reference. */ if (c->data == NULL) - ret = PyObject_CallFunction(c->callback, "iN", severity, text); /* New reference. */ + ret = PyObject_CallFunction(c->callback, "iN", severity, text); /* New reference. Steals a reference from "text". */ else - ret = PyObject_CallFunction(c->callback, "iNO", severity, text, c->data); /* New reference. */ + ret = PyObject_CallFunction(c->callback, "iNO", severity, text, c->data); /* New reference. Steals a reference from "text". */ if (ret == NULL) { /* FIXME */ @@ -525,12 +523,13 @@ static void cpy_flush_callback(int timeout, const char *id, user_data_t *data) { static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args, PyObject *kwds) { char buf[512]; cpy_callback_t *c; - const char *name = NULL; + char *name = NULL; PyObject *callback = NULL, *data = NULL, *mod = NULL; static char *kwlist[] = {"callback", "data", "name", NULL}; if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oet", kwlist, &callback, &data, NULL, &name) == 0) return NULL; if (PyCallable_Check(callback) == 0) { + PyMem_Free(name); PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object."); return NULL; } @@ -545,18 +544,21 @@ static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args c->next = *list_head; *list_head = c; Py_XDECREF(mod); + PyMem_Free(name); return cpy_string_to_unicode_or_bytes(buf); } static PyObject *cpy_flush(cpy_callback_t **list_head, PyObject *args, PyObject *kwds) { int timeout = -1; - const char *plugin = NULL, *identifier = NULL; + char *plugin = NULL, *identifier = NULL; static char *kwlist[] = {"plugin", "timeout", "identifier", NULL}; if (PyArg_ParseTupleAndKeywords(args, kwds, "|etiet", kwlist, NULL, &plugin, &timeout, NULL, &identifier) == 0) return NULL; Py_BEGIN_ALLOW_THREADS plugin_flush(plugin, timeout, identifier); Py_END_ALLOW_THREADS + PyMem_Free(plugin); + PyMem_Free(identifier); Py_RETURN_NONE; } @@ -575,16 +577,18 @@ static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObjec reg_function_t *register_function = (reg_function_t *) reg; cpy_callback_t *c = NULL; user_data_t *user_data = NULL; - const char *name = NULL; + char *name = NULL; PyObject *callback = NULL, *data = NULL; static char *kwlist[] = {"callback", "data", "name", NULL}; if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oet", kwlist, &callback, &data, NULL, &name) == 0) return NULL; if (PyCallable_Check(callback) == 0) { + PyMem_Free(name); PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object."); return NULL; } cpy_build_name(buf, sizeof(buf), callback, name); + PyMem_Free(name); Py_INCREF(callback); Py_XINCREF(data); @@ -605,17 +609,19 @@ static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwd cpy_callback_t *c = NULL; user_data_t *user_data = NULL; double interval = 0; - const char *name = NULL; + char *name = NULL; PyObject *callback = NULL, *data = NULL; struct timespec ts; static char *kwlist[] = {"callback", "interval", "data", "name", NULL}; if (PyArg_ParseTupleAndKeywords(args, kwds, "O|dOet", kwlist, &callback, &interval, &data, NULL, &name) == 0) return NULL; if (PyCallable_Check(callback) == 0) { + PyMem_Free(name); PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object."); return NULL; } cpy_build_name(buf, sizeof(buf), callback, name); + PyMem_Free(name); Py_INCREF(callback); Py_XINCREF(data); @@ -659,48 +665,53 @@ static PyObject *cpy_register_shutdown(PyObject *self, PyObject *args, PyObject } static PyObject *cpy_error(PyObject *self, PyObject *args) { - const char *text; + char *text; if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL; Py_BEGIN_ALLOW_THREADS plugin_log(LOG_ERR, "%s", text); Py_END_ALLOW_THREADS + PyMem_Free(text); Py_RETURN_NONE; } static PyObject *cpy_warning(PyObject *self, PyObject *args) { - const char *text; + char *text; if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL; Py_BEGIN_ALLOW_THREADS plugin_log(LOG_WARNING, "%s", text); Py_END_ALLOW_THREADS + PyMem_Free(text); Py_RETURN_NONE; } static PyObject *cpy_notice(PyObject *self, PyObject *args) { - const char *text; + char *text; if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL; Py_BEGIN_ALLOW_THREADS plugin_log(LOG_NOTICE, "%s", text); Py_END_ALLOW_THREADS + PyMem_Free(text); Py_RETURN_NONE; } static PyObject *cpy_info(PyObject *self, PyObject *args) { - const char *text; + char *text; if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL; Py_BEGIN_ALLOW_THREADS plugin_log(LOG_INFO, "%s", text); Py_END_ALLOW_THREADS + PyMem_Free(text); Py_RETURN_NONE; } static PyObject *cpy_debug(PyObject *self, PyObject *args) { #ifdef COLLECT_DEBUG - const char *text; + char *text; if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL; Py_BEGIN_ALLOW_THREADS plugin_log(LOG_DEBUG, "%s", text); Py_END_ALLOW_THREADS + PyMem_Free(text); #endif Py_RETURN_NONE; } diff --git a/src/pyvalues.c b/src/pyvalues.c index 4a701d01..fd078822 100644 --- a/src/pyvalues.c +++ b/src/pyvalues.c @@ -32,6 +32,14 @@ #include "cpython.h" +#define FreeAll() do {\ + PyMem_Free(type);\ + PyMem_Free(plugin_instance);\ + PyMem_Free(type_instance);\ + PyMem_Free(plugin);\ + PyMem_Free(host);\ +} while(0) + static PyObject *cpy_common_repr(PyObject *s) { PyObject *ret, *tmp; static PyObject *l_type = NULL, *l_type_instance = NULL, *l_plugin = NULL, *l_plugin_instance = NULL; @@ -143,7 +151,7 @@ static PyObject *PluginData_new(PyTypeObject *type, PyObject *args, PyObject *kw static int PluginData_init(PyObject *s, PyObject *args, PyObject *kwds) { PluginData *self = (PluginData *) s; double time = 0; - const char *type = "", *plugin_instance = "", *type_instance = "", *plugin = "", *host = ""; + char *type = NULL, *plugin_instance = NULL, *type_instance = NULL, *plugin = NULL, *host = NULL; static char *kwlist[] = {"type", "plugin_instance", "type_instance", "plugin", "host", "time", NULL}; @@ -151,18 +159,21 @@ static int PluginData_init(PyObject *s, PyObject *args, PyObject *kwds) { NULL, &plugin_instance, NULL, &type_instance, NULL, &plugin, NULL, &host, &time)) return -1; - if (type[0] != 0 && plugin_get_ds(type) == NULL) { + if (type && plugin_get_ds(type) == NULL) { PyErr_Format(PyExc_TypeError, "Dataset %s not found", type); + FreeAll(); return -1; } - sstrncpy(self->host, host, sizeof(self->host)); - sstrncpy(self->plugin, plugin, sizeof(self->plugin)); - sstrncpy(self->plugin_instance, plugin_instance, sizeof(self->plugin_instance)); - sstrncpy(self->type, type, sizeof(self->type)); - sstrncpy(self->type_instance, type_instance, sizeof(self->type_instance)); - + sstrncpy(self->host, host ? host : "", sizeof(self->host)); + sstrncpy(self->plugin, plugin ? plugin : "", sizeof(self->plugin)); + sstrncpy(self->plugin_instance, plugin_instance ? plugin_instance : "", sizeof(self->plugin_instance)); + sstrncpy(self->type, type ? type : "", sizeof(self->type)); + sstrncpy(self->type_instance, type_instance ? type_instance : "", sizeof(self->type_instance)); self->time = time; + + FreeAll(); + return 0; } @@ -354,7 +365,7 @@ static int Values_init(PyObject *s, PyObject *args, PyObject *kwds) { int interval = 0; double time = 0; PyObject *values = NULL, *meta = NULL, *tmp; - const char *type = "", *plugin_instance = "", *type_instance = "", *plugin = "", *host = ""; + char *type = NULL, *plugin_instance = NULL, *type_instance = NULL, *plugin = NULL, *host = NULL; static char *kwlist[] = {"type", "values", "plugin_instance", "type_instance", "plugin", "host", "time", "interval", "meta", NULL}; @@ -363,18 +374,21 @@ static int Values_init(PyObject *s, PyObject *args, PyObject *kwds) { NULL, &plugin, NULL, &host, &time, &interval, &meta)) return -1; - if (type[0] != 0 && plugin_get_ds(type) == NULL) { + if (type && plugin_get_ds(type) == NULL) { PyErr_Format(PyExc_TypeError, "Dataset %s not found", type); + FreeAll(); return -1; } - sstrncpy(self->data.host, host, sizeof(self->data.host)); - sstrncpy(self->data.plugin, plugin, sizeof(self->data.plugin)); - sstrncpy(self->data.plugin_instance, plugin_instance, sizeof(self->data.plugin_instance)); - sstrncpy(self->data.type, type, sizeof(self->data.type)); - sstrncpy(self->data.type_instance, type_instance, sizeof(self->data.type_instance)); + sstrncpy(self->data.host, host ? host : "", sizeof(self->data.host)); + sstrncpy(self->data.plugin, plugin ? plugin : "", sizeof(self->data.plugin)); + sstrncpy(self->data.plugin_instance, plugin_instance ? plugin_instance : "", sizeof(self->data.plugin_instance)); + sstrncpy(self->data.type, type ? type : "", sizeof(self->data.type)); + sstrncpy(self->data.type_instance, type_instance ? type_instance : "", sizeof(self->data.type_instance)); self->data.time = time; + FreeAll(); + if (values == NULL) { values = PyList_New(0); PyErr_Clear(); @@ -492,11 +506,7 @@ static PyObject *Values_dispatch(Values *self, PyObject *args, PyObject *kwds) { PyObject *values = self->values, *meta = self->meta; double time = self->data.time; int interval = self->interval; - const char *host = self->data.host; - const char *plugin = self->data.plugin; - const char *plugin_instance = self->data.plugin_instance; - const char *type = self->data.type; - const char *type_instance = self->data.type_instance; + char *host = NULL, *plugin = NULL, *plugin_instance = NULL, *type = NULL, *type_instance = NULL; static char *kwlist[] = {"type", "values", "plugin_instance", "type_instance", "plugin", "host", "time", "interval", "meta", NULL}; @@ -505,13 +515,20 @@ static PyObject *Values_dispatch(Values *self, PyObject *args, PyObject *kwds) { NULL, &plugin, NULL, &host, &time, &interval, &meta)) return NULL; - if (type[0] == 0) { + sstrncpy(value_list.host, host ? host : self->data.host, sizeof(value_list.host)); + sstrncpy(value_list.plugin, plugin ? plugin : self->data.plugin, sizeof(value_list.plugin)); + sstrncpy(value_list.plugin_instance, plugin_instance ? plugin_instance : self->data.plugin_instance, sizeof(value_list.plugin_instance)); + sstrncpy(value_list.type, type ? type : self->data.type, sizeof(value_list.type)); + sstrncpy(value_list.type_instance, type_instance ? type_instance : self->data.type_instance, sizeof(value_list.type_instance)); + FreeAll(); + if (value_list.type[0] == 0) { PyErr_SetString(PyExc_RuntimeError, "type not set"); + FreeAll(); return NULL; } - ds = plugin_get_ds(type); + ds = plugin_get_ds(value_list.type); if (ds == NULL) { - PyErr_Format(PyExc_TypeError, "Dataset %s not found", type); + PyErr_Format(PyExc_TypeError, "Dataset %s not found", value_list.type); return NULL; } if (values == NULL || (PyTuple_Check(values) == 0 && PyList_Check(values) == 0)) { @@ -524,7 +541,7 @@ static PyObject *Values_dispatch(Values *self, PyObject *args, PyObject *kwds) { } size = (int) PySequence_Length(values); if (size != ds->ds_num) { - PyErr_Format(PyExc_RuntimeError, "type %s needs %d values, got %i", type, ds->ds_num, size); + PyErr_Format(PyExc_RuntimeError, "type %s needs %d values, got %i", value_list.type, ds->ds_num, size); return NULL; } value = malloc(size * sizeof(*value)); @@ -561,7 +578,7 @@ static PyObject *Values_dispatch(Values *self, PyObject *args, PyObject *kwds) { } } else { free(value); - PyErr_Format(PyExc_RuntimeError, "unknown data type %d for %s", ds->ds->type, type); + PyErr_Format(PyExc_RuntimeError, "unknown data type %d for %s", ds->ds->type, value_list.type); return NULL; } if (PyErr_Occurred() != NULL) { @@ -574,11 +591,6 @@ static PyObject *Values_dispatch(Values *self, PyObject *args, PyObject *kwds) { value_list.values_len = size; value_list.time = time; value_list.interval = interval; - sstrncpy(value_list.host, host, sizeof(value_list.host)); - sstrncpy(value_list.plugin, plugin, sizeof(value_list.plugin)); - sstrncpy(value_list.plugin_instance, plugin_instance, sizeof(value_list.plugin_instance)); - sstrncpy(value_list.type, type, sizeof(value_list.type)); - sstrncpy(value_list.type_instance, type_instance, sizeof(value_list.type_instance)); if (value_list.host[0] == 0) sstrncpy(value_list.host, hostname_g, sizeof(value_list.host)); if (value_list.plugin[0] == 0) @@ -604,27 +616,28 @@ static PyObject *Values_write(Values *self, PyObject *args, PyObject *kwds) { PyObject *values = self->values, *meta = self->meta; double time = self->data.time; int interval = self->interval; - const char *host = self->data.host; - const char *plugin = self->data.plugin; - const char *plugin_instance = self->data.plugin_instance; - const char *type = self->data.type; - const char *type_instance = self->data.type_instance; - const char *dest = NULL; + char *host = NULL, *plugin = NULL, *plugin_instance = NULL, *type = NULL, *type_instance = NULL, *dest = NULL; static char *kwlist[] = {"destination", "type", "values", "plugin_instance", "type_instance", "plugin", "host", "time", "interval", "meta", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etOetetetetdiO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|etOetetetetdiO", kwlist, NULL, &dest, NULL, &type, &values, NULL, &plugin_instance, NULL, &type_instance, NULL, &plugin, NULL, &host, &time, &interval, &meta)) return NULL; - if (type[0] == 0) { + sstrncpy(value_list.host, host ? host : self->data.host, sizeof(value_list.host)); + sstrncpy(value_list.plugin, plugin ? plugin : self->data.plugin, sizeof(value_list.plugin)); + sstrncpy(value_list.plugin_instance, plugin_instance ? plugin_instance : self->data.plugin_instance, sizeof(value_list.plugin_instance)); + sstrncpy(value_list.type, type ? type : self->data.type, sizeof(value_list.type)); + sstrncpy(value_list.type_instance, type_instance ? type_instance : self->data.type_instance, sizeof(value_list.type_instance)); + FreeAll(); + if (value_list.type[0] == 0) { PyErr_SetString(PyExc_RuntimeError, "type not set"); return NULL; } - ds = plugin_get_ds(type); + ds = plugin_get_ds(value_list.type); if (ds == NULL) { - PyErr_Format(PyExc_TypeError, "Dataset %s not found", type); + PyErr_Format(PyExc_TypeError, "Dataset %s not found", value_list.type); return NULL; } if (values == NULL || (PyTuple_Check(values) == 0 && PyList_Check(values) == 0)) { @@ -633,7 +646,7 @@ static PyObject *Values_write(Values *self, PyObject *args, PyObject *kwds) { } size = (int) PySequence_Length(values); if (size != ds->ds_num) { - PyErr_Format(PyExc_RuntimeError, "type %s needs %d values, got %i", type, ds->ds_num, size); + PyErr_Format(PyExc_RuntimeError, "type %s needs %d values, got %i", value_list.type, ds->ds_num, size); return NULL; } value = malloc(size * sizeof(*value)); @@ -670,7 +683,7 @@ static PyObject *Values_write(Values *self, PyObject *args, PyObject *kwds) { } } else { free(value); - PyErr_Format(PyExc_RuntimeError, "unknown data type %d for %s", ds->ds->type, type); + PyErr_Format(PyExc_RuntimeError, "unknown data type %d for %s", ds->ds->type, value_list.type); return NULL; } if (PyErr_Occurred() != NULL) { @@ -682,11 +695,6 @@ static PyObject *Values_write(Values *self, PyObject *args, PyObject *kwds) { value_list.values_len = size; value_list.time = time; value_list.interval = interval; - sstrncpy(value_list.host, host, sizeof(value_list.host)); - sstrncpy(value_list.plugin, plugin, sizeof(value_list.plugin)); - sstrncpy(value_list.plugin_instance, plugin_instance, sizeof(value_list.plugin_instance)); - sstrncpy(value_list.type, type, sizeof(value_list.type)); - sstrncpy(value_list.type_instance, type_instance, sizeof(value_list.type_instance)); value_list.meta = cpy_build_meta(meta);; if (value_list.host[0] == 0) sstrncpy(value_list.host, hostname_g, sizeof(value_list.host)); @@ -830,8 +838,8 @@ static int Notification_init(PyObject *s, PyObject *args, PyObject *kwds) { Notification *self = (Notification *) s; int severity = 0; double time = 0; - const char *message = ""; - const char *type = "", *plugin_instance = "", *type_instance = "", *plugin = "", *host = ""; + char *message = NULL; + char *type = NULL, *plugin_instance = NULL, *type_instance = NULL, *plugin = NULL, *host = NULL; static char *kwlist[] = {"type", "message", "plugin_instance", "type_instance", "plugin", "host", "time", "severity", NULL}; @@ -840,20 +848,24 @@ static int Notification_init(PyObject *s, PyObject *args, PyObject *kwds) { NULL, &plugin, NULL, &host, &time, &severity)) return -1; - if (type[0] != 0 && plugin_get_ds(type) == NULL) { + if (type && plugin_get_ds(type) == NULL) { PyErr_Format(PyExc_TypeError, "Dataset %s not found", type); + FreeAll(); + PyMem_Free(message); return -1; } - sstrncpy(self->data.host, host, sizeof(self->data.host)); - sstrncpy(self->data.plugin, plugin, sizeof(self->data.plugin)); - sstrncpy(self->data.plugin_instance, plugin_instance, sizeof(self->data.plugin_instance)); - sstrncpy(self->data.type, type, sizeof(self->data.type)); - sstrncpy(self->data.type_instance, type_instance, sizeof(self->data.type_instance)); + sstrncpy(self->data.host, host ? host : "", sizeof(self->data.host)); + sstrncpy(self->data.plugin, plugin ? plugin : "", sizeof(self->data.plugin)); + sstrncpy(self->data.plugin_instance, plugin_instance ? plugin_instance : "", sizeof(self->data.plugin_instance)); + sstrncpy(self->data.type, type ? type : "", sizeof(self->data.type)); + sstrncpy(self->data.type_instance, type_instance ? type_instance : "", sizeof(self->data.type_instance)); + sstrncpy(self->message, message ? message : "", sizeof(self->message)); self->data.time = time; - - sstrncpy(self->message, message, sizeof(self->message)); self->severity = severity; + + FreeAll(); + PyMem_Free(message); return 0; } @@ -863,12 +875,8 @@ static PyObject *Notification_dispatch(Notification *self, PyObject *args, PyObj notification_t notification; double t = self->data.time; int severity = self->severity; - const char *host = self->data.host; - const char *plugin = self->data.plugin; - const char *plugin_instance = self->data.plugin_instance; - const char *type = self->data.type; - const char *type_instance = self->data.type_instance; - const char *message = self->message; + char *host = NULL, *plugin = NULL, *plugin_instance = NULL, *type = NULL, *type_instance = NULL; + char *message = NULL; static char *kwlist[] = {"type", "message", "plugin_instance", "type_instance", "plugin", "host", "time", "severity", NULL}; @@ -877,25 +885,28 @@ static PyObject *Notification_dispatch(Notification *self, PyObject *args, PyObj NULL, &plugin, NULL, &host, &t, &severity)) return NULL; - if (type[0] == 0) { + notification.time = t; + notification.severity = severity; + sstrncpy(notification.message, message ? message : self->message, sizeof(notification.message)); + sstrncpy(notification.host, host ? host : self->data.host, sizeof(notification.host)); + sstrncpy(notification.plugin, plugin ? plugin : self->data.plugin, sizeof(notification.plugin)); + sstrncpy(notification.plugin_instance, plugin_instance ? plugin_instance : self->data.plugin_instance, sizeof(notification.plugin_instance)); + sstrncpy(notification.type, type ? type : self->data.type, sizeof(notification.type)); + sstrncpy(notification.type_instance, type_instance ? type_instance : self->data.type_instance, sizeof(notification.type_instance)); + notification.meta = NULL; + FreeAll(); + PyMem_Free(message); + + if (notification.type[0] == 0) { PyErr_SetString(PyExc_RuntimeError, "type not set"); return NULL; } - ds = plugin_get_ds(type); + ds = plugin_get_ds(notification.type); if (ds == NULL) { - PyErr_Format(PyExc_TypeError, "Dataset %s not found", type); + PyErr_Format(PyExc_TypeError, "Dataset %s not found", notification.type); return NULL; } - notification.time = t; - notification.severity = severity; - sstrncpy(notification.message, message, sizeof(notification.message)); - sstrncpy(notification.host, host, sizeof(notification.host)); - sstrncpy(notification.plugin, plugin, sizeof(notification.plugin)); - sstrncpy(notification.plugin_instance, plugin_instance, sizeof(notification.plugin_instance)); - sstrncpy(notification.type, type, sizeof(notification.type)); - sstrncpy(notification.type_instance, type_instance, sizeof(notification.type_instance)); - notification.meta = NULL; if (notification.time < 1) notification.time = time(0); if (notification.host[0] == 0) -- 2.11.0