pidgin/pidgin

Fix leaks in dynamic menus

16 months ago, Elliott Sales de Andrade
33ed4a2a7593
Fix leaks in dynamic menus

Fixes two leaks:
```
10,463 (48 direct, 10,415 indirect) bytes in 1 blocks are definitely lost in loss record 34,138 of 34,479
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x4980168: g_malloc (gmem.c:130)
by 0x4997AB5: g_slice_alloc (gslice.c:1074)
by 0x49980EC: g_slice_alloc0 (gslice.c:1100)
by 0x4DDF04B: g_type_create_instance (gtype.c:1913)
by 0x4DC4C1F: g_object_new_internal (gobject.c:2228)
by 0x4DC6247: g_object_new_with_properties (gobject.c:2391)
by 0x4DC6FF0: g_object_new (gobject.c:2037)
by 0x1CA978CF: purple_demo_protocol_get_menu (purpledemoprotocolactions.c:750)
by 0x4D2DB1F: purple_protocol_actions_get_menu (purpleprotocolactions.c:110)
by 0x489F74C: pidgin_accounts_enabled_menu_get_item_links (pidginaccountsenabledmenu.c:279)
by 0x5817C3F: g_menu_model_real_get_item_link (gmenumodel.c:397)
```
and
```
2,051 (144 direct, 1,907 indirect) bytes in 3 blocks are definitely lost in loss record 33,018 of 34,479
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x4980168: g_malloc (gmem.c:130)
by 0x4997AB5: g_slice_alloc (gslice.c:1074)
by 0x49980EC: g_slice_alloc0 (gslice.c:1100)
by 0x4DDF04B: g_type_create_instance (gtype.c:1913)
by 0x4DC4C1F: g_object_new_internal (gobject.c:2228)
by 0x4DC6247: g_object_new_with_properties (gobject.c:2391)
by 0x4DC6FF0: g_object_new (gobject.c:2037)
by 0x581E9EE: g_menu_item_new_section (gmenu.c:1256)
by 0x48B465F: pidgin_plugins_menu_get_item_links (pidginpluginsmenu.c:204)
by 0x5817C3F: g_menu_model_real_get_item_link (gmenumodel.c:397)
by 0x5124B78: gtk_menu_tracker_add_items (gtkmenutracker.c:313)
```

Testing Done:
Ran in `valgrind --leak-check=full` and confirmed that the leak was gone.

Reviewed at https://reviews.imfreedom.org/r/2298/
/*
* Purple - Internet Messaging Library
* Copyright (C) Pidgin Developers <devel@pidgin.im>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <https://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <purple.h>
typedef struct {
const gchar *dynamic_target;
const gchar *target;
} TestPurpleMenuItemData;
typedef struct {
GList *items;
} TestPurpleMenuWalkData;
/******************************************************************************
* Helpers
*****************************************************************************/
static TestPurpleMenuItemData *
test_purple_menu_item_new(const gchar *dynamic_target, const gchar *target) {
TestPurpleMenuItemData *data = g_new(TestPurpleMenuItemData, 1);
data->dynamic_target = dynamic_target;
data->target = target;
return data;
}
static void
test_purple_menu_items_func(GMenuModel *model, gint index, gpointer data) {
TestPurpleMenuItemData *item = NULL;
TestPurpleMenuWalkData *walk_data = data;
gchar *actual = NULL;
gboolean found = FALSE;
/* Set item to the first item in the list. */
item = walk_data->items->data;
/* Check that the dynamic-target value matches our expectations. */
g_menu_model_get_item_attribute(model, index,
PURPLE_MENU_ATTRIBUTE_DYNAMIC_TARGET, "s",
&actual);
g_assert_cmpstr(item->dynamic_target, ==, actual);
g_free(actual);
/* Check that the target value matches our expectations. */
found = g_menu_model_get_item_attribute(model, index,
G_MENU_ATTRIBUTE_TARGET, "s",
&actual);
if(item->target != NULL) {
g_assert_true(found);
g_assert_cmpstr(item->target, ==, actual);
g_free(actual);
} else {
g_assert_false(found);
}
/* Free our data and move to the next item. */
g_free(item);
walk_data->items = g_list_delete_link(walk_data->items, walk_data->items);
}
static void
test_purple_menu_items(GMenu *menu, GList *items) {
TestPurpleMenuWalkData data = {
.items = items,
};
purple_menu_walk(G_MENU_MODEL(menu), test_purple_menu_items_func, &data);
g_assert_null(data.items);
}
/******************************************************************************
* Tests
*****************************************************************************/
static void
test_purple_menu_single_no_dynamic_target(void) {
GMenu *menu = g_menu_new();
GList *expected = NULL;
g_menu_append(menu, "item1", NULL);
expected = g_list_append(expected, test_purple_menu_item_new(NULL, NULL));
purple_menu_populate_dynamic_targets(menu, "property", "1", NULL);
test_purple_menu_items(menu, expected);
g_object_unref(menu);
}
static void
test_purple_menu_single_unset_dynamic_target(void) {
GMenu *menu = g_menu_new();
GMenuItem *item = NULL;
GList *expected = NULL;
item = g_menu_item_new("item1", NULL);
g_menu_item_set_attribute(item, PURPLE_MENU_ATTRIBUTE_DYNAMIC_TARGET, "s",
"foo");
g_menu_append_item(menu, item);
g_object_unref(item);
expected = g_list_append(expected, test_purple_menu_item_new("foo", NULL));
purple_menu_populate_dynamic_targets(menu, "bar", "123", NULL);
test_purple_menu_items(menu, expected);
g_object_unref(menu);
}
static void
test_purple_menu_single_single_dynamic_target(void) {
GMenu *menu = g_menu_new();
GMenuItem *item = NULL;
GList *expected = NULL;
item = g_menu_item_new("item1", NULL);
g_menu_item_set_attribute(item, PURPLE_MENU_ATTRIBUTE_DYNAMIC_TARGET, "s",
"foo");
g_menu_append_item(menu, item);
g_object_unref(item);
expected = g_list_append(expected, test_purple_menu_item_new("foo", "bar"));
purple_menu_populate_dynamic_targets(menu, "foo", "bar", NULL);
test_purple_menu_items(menu, expected);
g_object_unref(menu);
}
static void
test_purple_menu_section_single(void) {
GMenu *menu = NULL, *section = NULL;
GMenuItem *item = NULL;
GList *expected = NULL;
/* Create our section. */
section = g_menu_new();
expected = g_list_append(expected, test_purple_menu_item_new(NULL, NULL));
/* Create our item and add it to the list. */
item = g_menu_item_new("item1", NULL);
g_menu_item_set_attribute(item, PURPLE_MENU_ATTRIBUTE_DYNAMIC_TARGET, "s",
"foo");
g_menu_append_item(section, item);
g_object_unref(item);
expected = g_list_append(expected, test_purple_menu_item_new("foo", "bar"));
/* Finally add our section to our menu. */
menu = g_menu_new();
g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
g_object_unref(section);
purple_menu_populate_dynamic_targets(menu, "foo", "bar", NULL);
test_purple_menu_items(menu, expected);
g_object_unref(menu);
}
static void
test_purple_menu_multiple_multiple_dynamic_target(void) {
GMenu *menu = g_menu_new();
GMenuItem *item = NULL;
GList *expected = NULL;
item = g_menu_item_new("item1", NULL);
g_menu_item_set_attribute(item, PURPLE_MENU_ATTRIBUTE_DYNAMIC_TARGET, "s",
"foo");
g_menu_append_item(menu, item);
g_object_unref(item);
expected = g_list_append(expected, test_purple_menu_item_new("foo", "bar"));
item = g_menu_item_new("item2", NULL);
g_menu_item_set_attribute(item, PURPLE_MENU_ATTRIBUTE_DYNAMIC_TARGET, "s",
"abc");
g_menu_append_item(menu, item);
g_object_unref(item);
expected = g_list_append(expected, test_purple_menu_item_new("abc", "123"));
purple_menu_populate_dynamic_targets(menu, "foo", "bar", "abc", "123",
NULL);
test_purple_menu_items(menu, expected);
g_object_unref(menu);
}
static void
test_purple_menu_multiple_mixed(void) {
GMenu *menu = g_menu_new();
GMenuItem *item = NULL;
GList *expected = NULL;
g_menu_append(menu, "item1", NULL);
expected = g_list_append(expected, test_purple_menu_item_new(NULL, NULL));
item = g_menu_item_new("item2", NULL);
g_menu_item_set_attribute(item, PURPLE_MENU_ATTRIBUTE_DYNAMIC_TARGET, "s",
"foo");
g_menu_append_item(menu, item);
g_object_unref(item);
expected = g_list_append(expected, test_purple_menu_item_new("foo", "bar"));
item = g_menu_item_new("item3", NULL);
g_menu_item_set_attribute(item, PURPLE_MENU_ATTRIBUTE_DYNAMIC_TARGET, "s",
"abc");
g_menu_append_item(menu, item);
g_object_unref(item);
expected = g_list_append(expected, test_purple_menu_item_new("abc", "123"));
purple_menu_populate_dynamic_targets(menu, "foo", "bar", "abc", "123",
NULL);
test_purple_menu_items(menu, expected);
g_object_unref(menu);
}
/******************************************************************************
* Main
*****************************************************************************/
gint
main(gint argc, gchar *argv[]) {
g_test_init(&argc, &argv, NULL);
g_test_add_func("/menu/single/no-dynamic-target",
test_purple_menu_single_no_dynamic_target);
g_test_add_func("/menu/single/unset-dynamic-target",
test_purple_menu_single_unset_dynamic_target);
g_test_add_func("/menu/single/single_dynamic_target",
test_purple_menu_single_single_dynamic_target);
g_test_add_func("/menu/section/single",
test_purple_menu_section_single);
g_test_add_func("/menu/multiple/multiple_dynamic_target",
test_purple_menu_multiple_multiple_dynamic_target);
g_test_add_func("/menu/multiple/mixed",
test_purple_menu_multiple_mixed);
return g_test_run();
}