From 139e53f388e06a91bebddbf144556db05ea61898 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 25 Jul 2016 21:48:00 +0100 Subject: [PATCH 1/3] Decrement reference count on python hook args objects --- src/plugins/python_plugins.c | 62 +++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c index 16c64fab..988437f5 100644 --- a/src/plugins/python_plugins.c +++ b/src/plugins/python_plugins.c @@ -172,6 +172,7 @@ python_init_hook(ProfPlugin *plugin, const char *const version, const char *cons Py_XDECREF(p_function); } } + Py_XDECREF(p_args); allow_python_threads(); } @@ -189,6 +190,7 @@ python_on_start_hook(ProfPlugin *plugin) PyObject_CallObject(p_function, NULL); python_check_error(); Py_XDECREF(p_function); + } } allow_python_threads(); @@ -249,6 +251,7 @@ python_on_connect_hook(ProfPlugin *plugin, const char *const account_name, const Py_XDECREF(p_function); } } + Py_XDECREF(p_args); allow_python_threads(); } @@ -269,6 +272,7 @@ python_on_disconnect_hook(ProfPlugin *plugin, const char *const account_name, co Py_XDECREF(p_function); } } + Py_XDECREF(p_args); allow_python_threads(); } @@ -287,13 +291,14 @@ python_pre_chat_message_display_hook(ProfPlugin *plugin, const char *const jid, PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); char *result_str = python_str_or_unicode_to_string(result); allow_python_threads(); return result_str; } } - + Py_XDECREF(p_args); allow_python_threads(); return NULL; } @@ -315,7 +320,7 @@ python_post_chat_message_display_hook(ProfPlugin *plugin, const char *const jid, Py_XDECREF(p_function); } } - + Py_XDECREF(p_args); allow_python_threads(); } @@ -334,13 +339,14 @@ python_pre_chat_message_send_hook(ProfPlugin *plugin, const char * const jid, co PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); char *result_str = python_str_or_unicode_to_string(result); allow_python_threads(); return result_str; } } - + Py_XDECREF(p_args); allow_python_threads(); return NULL; } @@ -362,7 +368,7 @@ python_post_chat_message_send_hook(ProfPlugin *plugin, const char *const jid, co Py_XDECREF(p_function); } } - + Py_XDECREF(p_args); allow_python_threads(); } @@ -381,13 +387,14 @@ python_pre_room_message_display_hook(ProfPlugin *plugin, const char * const room PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); char *result_str = python_str_or_unicode_to_string(result); allow_python_threads(); return result_str; } } - + Py_XDECREF(p_args); allow_python_threads(); return NULL; } @@ -410,7 +417,7 @@ python_post_room_message_display_hook(ProfPlugin *plugin, const char *const room Py_XDECREF(p_function); } } - + Py_XDECREF(p_args); allow_python_threads(); } @@ -429,13 +436,14 @@ python_pre_room_message_send_hook(ProfPlugin *plugin, const char *const room, co PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); char *result_str = python_str_or_unicode_to_string(result); allow_python_threads(); return result_str; } } - + Py_XDECREF(p_args); allow_python_threads(); return NULL; } @@ -457,7 +465,7 @@ python_post_room_message_send_hook(ProfPlugin *plugin, const char *const room, c Py_XDECREF(p_function); } } - + Py_XDECREF(p_args); allow_python_threads(); } @@ -479,7 +487,7 @@ python_on_room_history_message_hook(ProfPlugin *plugin, const char *const room, Py_XDECREF(p_function); } } - + Py_XDECREF(p_args); allow_python_threads(); } @@ -499,13 +507,14 @@ python_pre_priv_message_display_hook(ProfPlugin *plugin, const char *const room, PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); char *result_str = python_str_or_unicode_to_string(result); allow_python_threads(); return result_str; } } - + Py_XDECREF(p_args); allow_python_threads(); return NULL; } @@ -528,7 +537,7 @@ python_post_priv_message_display_hook(ProfPlugin *plugin, const char *const room Py_XDECREF(p_function); } } - + Py_XDECREF(p_args); allow_python_threads(); } @@ -548,13 +557,14 @@ python_pre_priv_message_send_hook(ProfPlugin *plugin, const char *const room, co PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); char *result_str = python_str_or_unicode_to_string(result); allow_python_threads(); return result_str; } } - + Py_XDECREF(p_args); allow_python_threads(); return NULL; } @@ -577,7 +587,7 @@ python_post_priv_message_send_hook(ProfPlugin *plugin, const char *const room, c Py_XDECREF(p_function); } } - + Py_XDECREF(p_args); allow_python_threads(); } @@ -596,13 +606,14 @@ python_on_message_stanza_send_hook(ProfPlugin *plugin, const char *const text) PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); char *result_str = python_str_or_unicode_to_string(result); allow_python_threads(); return result_str; } } - + Py_XDECREF(p_args); allow_python_threads(); return NULL; } @@ -622,6 +633,7 @@ python_on_message_stanza_receive_hook(ProfPlugin *plugin, const char *const text PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); if (PyObject_IsTrue(result)) { allow_python_threads(); return TRUE; @@ -631,7 +643,7 @@ python_on_message_stanza_receive_hook(ProfPlugin *plugin, const char *const text } } } - + Py_XDECREF(p_args); allow_python_threads(); return TRUE; } @@ -651,13 +663,14 @@ python_on_presence_stanza_send_hook(ProfPlugin *plugin, const char *const text) PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); char *result_str = python_str_or_unicode_to_string(result); allow_python_threads(); return result_str; } } - + Py_XDECREF(p_args); allow_python_threads(); return NULL; } @@ -677,6 +690,7 @@ python_on_presence_stanza_receive_hook(ProfPlugin *plugin, const char *const tex PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); if (PyObject_IsTrue(result)) { allow_python_threads(); return TRUE; @@ -686,7 +700,7 @@ python_on_presence_stanza_receive_hook(ProfPlugin *plugin, const char *const tex } } } - + Py_XDECREF(p_args); allow_python_threads(); return TRUE; } @@ -706,13 +720,14 @@ python_on_iq_stanza_send_hook(ProfPlugin *plugin, const char *const text) PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); char *result_str = python_str_or_unicode_to_string(result); allow_python_threads(); return result_str; } } - + Py_XDECREF(p_args); allow_python_threads(); return NULL; } @@ -732,6 +747,7 @@ python_on_iq_stanza_receive_hook(ProfPlugin *plugin, const char *const text) PyObject *result = PyObject_CallObject(p_function, p_args); python_check_error(); Py_XDECREF(p_function); + Py_XDECREF(p_args); if (PyObject_IsTrue(result)) { allow_python_threads(); return TRUE; @@ -741,7 +757,7 @@ python_on_iq_stanza_receive_hook(ProfPlugin *plugin, const char *const text) } } } - + Py_XDECREF(p_args); allow_python_threads(); return TRUE; } @@ -764,7 +780,7 @@ python_on_contact_offline_hook(ProfPlugin *plugin, const char *const barejid, co Py_XDECREF(p_function); } } - + Py_XDECREF(p_args); allow_python_threads(); } @@ -786,7 +802,7 @@ python_on_contact_presence_hook(ProfPlugin *plugin, const char *const barejid, c Py_XDECREF(p_function); } } - + Py_XDECREF(p_args); allow_python_threads(); } @@ -807,7 +823,7 @@ python_on_chat_win_focus_hook(ProfPlugin *plugin, const char *const barejid) Py_XDECREF(p_function); } } - + Py_XDECREF(p_args); allow_python_threads(); } @@ -828,7 +844,7 @@ python_on_room_win_focus_hook(ProfPlugin *plugin, const char *const roomjid) Py_XDECREF(p_function); } } - + Py_XDECREF(p_args); allow_python_threads(); } From 943e91268c2489600ac1f28836fd0e24a0fd8dc2 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 25 Jul 2016 21:56:38 +0100 Subject: [PATCH 2/3] Fix memory leaks --- src/command/cmd_ac.c | 3 ++- src/plugins/python_api.c | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c index afc642e4..6cb7b1a1 100644 --- a/src/command/cmd_ac.c +++ b/src/command/cmd_ac.c @@ -2640,9 +2640,9 @@ _join_autocomplete(ProfWin *window, const char *const input) if (result) { gboolean space_at_end = g_str_has_suffix(input, " "); - GString *beginning = g_string_new("/join"); int num_args = g_strv_length(args); if ((num_args == 1 && space_at_end) || (num_args == 2 && !space_at_end)) { + GString *beginning = g_string_new("/join"); g_string_append_printf(beginning, " %s", args[0]); found = autocomplete_param_with_ac(input, beginning->str, join_property_ac, TRUE); g_string_free(beginning, TRUE); @@ -2652,6 +2652,7 @@ _join_autocomplete(ProfWin *window, const char *const input) } } if ((num_args == 3 && space_at_end) || (num_args == 4 && !space_at_end)) { + GString *beginning = g_string_new("/join"); g_string_append_printf(beginning, " %s %s %s", args[0], args[1], args[2]); found = autocomplete_param_with_ac(input, beginning->str, join_property_ac, TRUE); g_string_free(beginning, TRUE); diff --git a/src/plugins/python_api.c b/src/plugins/python_api.c index 8309e5ec..ee8a874c 100644 --- a/src/plugins/python_api.c +++ b/src/plugins/python_api.c @@ -740,7 +740,9 @@ python_api_settings_get_string(PyObject *self, PyObject *args) disable_python_threads(); if (res) { - return Py_BuildValue("s", res); + PyObject *pyres = Py_BuildValue("s", res); + free(res); + return pyres; } else { Py_RETURN_NONE; } From d5d04756ce278852cb465541f3b5bb4cf84f66ca Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 25 Jul 2016 22:23:54 +0100 Subject: [PATCH 3/3] Fix memory leaks --- src/log.c | 3 ++- src/plugins/callbacks.c | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/log.c b/src/log.c index 2c1bf7bc..e091f347 100644 --- a/src/log.c +++ b/src/log.c @@ -403,7 +403,7 @@ _chat_log_chat(const char *const login, const char *const other, const char *con void groupchat_log_chat(const gchar *const login, const gchar *const room, const gchar *const nick, const gchar *const msg) { - gchar *room_copy = strdup(room); + char *room_copy = strdup(room); struct dated_chat_log *dated_log = g_hash_table_lookup(groupchat_logs, room_copy); // no log for room @@ -416,6 +416,7 @@ groupchat_log_chat(const gchar *const login, const gchar *const room, const gcha dated_log = _create_groupchat_log(room_copy, login); g_hash_table_replace(logs, room_copy, dated_log); } + free(room_copy); GDateTime *dt = g_date_time_new_now_local(); diff --git a/src/plugins/callbacks.c b/src/plugins/callbacks.c index 40a35400..364cb669 100644 --- a/src/plugins/callbacks.c +++ b/src/plugins/callbacks.c @@ -299,6 +299,7 @@ plugins_run_command(const char * const input) curr_hash = g_list_next(curr_hash); } + g_list_free(command_hashes); g_strfreev(split); return FALSE; }