pidgin/pidgin

Fix leaks in purple markup

14 months ago, Elliott Sales de Andrade
a39305511805
Fix leaks in purple markup

Fix leaks when running the markup tests. These are all from leaking `tags`:
```
24 bytes in 1 blocks are definitely lost in loss record 101 of 251
at 0x4848464: calloc (vg_replace_malloc.c:1340)
by 0x49F75F0: g_malloc0 (gmem.c:163)
by 0x48E563C: purple_markup_html_to_xhtml (purplemarkup.c:437)
by 0x401214: test_purple_markup_html_to_xhtml (test_markup.c:213)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
by 0x4A156EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A156EC: g_test_run (gtestutils.c:2221)
by 0x401575: main (test_markup.c:281)

24 bytes in 1 blocks are definitely lost in loss record 102 of 251
at 0x4848464: calloc (vg_replace_malloc.c:1340)
by 0x49F75F0: g_malloc0 (gmem.c:163)
by 0x48E0319: purple_markup_html_to_xhtml (purplemarkup.c:260)
by 0x401214: test_purple_markup_html_to_xhtml (test_markup.c:213)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
by 0x4A156EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A156EC: g_test_run (gtestutils.c:2221)
by 0x401575: main (test_markup.c:281)

24 bytes in 1 blocks are definitely lost in loss record 103 of 251
at 0x4848464: calloc (vg_replace_malloc.c:1340)
by 0x49F75F0: g_malloc0 (gmem.c:163)
by 0x48E07B7: purple_markup_html_to_xhtml (purplemarkup.c:261)
by 0x401214: test_purple_markup_html_to_xhtml (test_markup.c:213)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
by 0x4A156EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A156EC: g_test_run (gtestutils.c:2221)
by 0x401575: main (test_markup.c:281)

24 bytes in 1 blocks are definitely lost in loss record 104 of 251
at 0x4848464: calloc (vg_replace_malloc.c:1340)
by 0x49F75F0: g_malloc0 (gmem.c:163)
by 0x48E0C55: purple_markup_html_to_xhtml (purplemarkup.c:262)
by 0x401214: test_purple_markup_html_to_xhtml (test_markup.c:213)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
by 0x4A156EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A156EC: g_test_run (gtestutils.c:2221)
by 0x401575: main (test_markup.c:281)

24 bytes in 1 blocks are definitely lost in loss record 105 of 251
at 0x4848464: calloc (vg_replace_malloc.c:1340)
by 0x49F75F0: g_malloc0 (gmem.c:163)
by 0x48E10F3: purple_markup_html_to_xhtml (purplemarkup.c:263)
by 0x401214: test_purple_markup_html_to_xhtml (test_markup.c:213)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
by 0x4A156EC: UnknownInlinedFun (gtestutils.c:2234)
by 0x4A156EC: g_test_run (gtestutils.c:2221)
by 0x401575: main (test_markup.c:281)
```
and these from not freeing the result from `purple_unescape_html`:
```
256 bytes in 2 blocks are definitely lost in loss record 241 of 251
at 0x484378A: malloc (vg_replace_malloc.c:392)
by 0x484870B: realloc (vg_replace_malloc.c:1451)
by 0x49F771F: g_realloc (gmem.c:201)
by 0x4A11343: g_string_maybe_expand (gstring.c:92)
by 0x4A113BF: g_string_sized_new (gstring.c:116)
by 0x48E7F74: purple_unescape_html (purplemarkup.c:1101)
by 0x48DEBBA: purple_markup_html_to_xhtml (purplemarkup.c:224)
by 0x401214: test_purple_markup_html_to_xhtml (test_markup.c:213)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)

256 bytes in 2 blocks are definitely lost in loss record 242 of 251
at 0x484378A: malloc (vg_replace_malloc.c:392)
by 0x484870B: realloc (vg_replace_malloc.c:1451)
by 0x49F771F: g_realloc (gmem.c:201)
by 0x4A11343: g_string_maybe_expand (gstring.c:92)
by 0x4A113BF: g_string_sized_new (gstring.c:116)
by 0x48E7F74: purple_unescape_html (purplemarkup.c:1101)
by 0x48E534D: purple_markup_html_to_xhtml (purplemarkup.c:397)
by 0x401214: test_purple_markup_html_to_xhtml (test_markup.c:213)
by 0x4A1CC7D: UnknownInlinedFun (gtestutils.c:2933)
by 0x4A1CC7D: g_test_run_suite_internal (gtestutils.c:3021)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1C9E4: g_test_run_suite_internal (gtestutils.c:3038)
by 0x4A1D181: g_test_run_suite (gtestutils.c:3115)
```

Also modified the test to check not passing the optional arguments.

Testing Done:
Compiled and ran tests in valgrind and leaks were gone.

Reviewed at https://reviews.imfreedom.org/r/2379/
/*
* Pidgin - Internet Messenger
* Copyright (C) Pidgin Developers <devel@pidgin.im>
*
* Pidgin is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
#include "pidgin/pidgininfopane.h"
#include "pidgin/gtkblist.h"
#include "pidgin/gtkutils.h"
#include "pidgin/pidginavatar.h"
#include "pidgin/pidgincore.h"
#include "pidgin/pidginpresenceicon.h"
struct _PidginInfoPane {
GtkBox parent;
PurpleConversation *conversation;
GtkWidget *hbox;
GtkWidget *presence_icon;
GtkWidget *name;
GtkWidget *topic;
GtkWidget *avatar;
};
enum {
PROP_0,
PROP_CONVERSATION,
N_PROPERTIES,
};
static GParamSpec *properties[N_PROPERTIES] = {NULL, };
G_DEFINE_TYPE(PidginInfoPane, pidgin_info_pane, GTK_TYPE_BOX)
/******************************************************************************
* Helpers
*****************************************************************************/
static void
pidgin_info_pane_set_conversation(PidginInfoPane *pane,
PurpleConversation *conversation)
{
if(!g_set_object(&pane->conversation, conversation)) {
return;
}
/* Tell the avatar about the new conversation. */
pidgin_avatar_set_conversation(PIDGIN_AVATAR(pane->avatar),
pane->conversation);
if(PURPLE_IS_CONVERSATION(conversation)) {
PidginPresenceIcon *presence_icon = NULL;
PurplePresence *presence = NULL;
const gchar *fallback = "person";
g_object_bind_property(G_OBJECT(conversation), "name",
G_OBJECT(pane->name), "label",
G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
if(PURPLE_IS_CHAT_CONVERSATION(conversation)) {
g_object_bind_property(G_OBJECT(conversation), "topic",
G_OBJECT(pane->topic), "label",
G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
fallback = "chat";
} else if(PURPLE_IS_IM_CONVERSATION(conversation)){
PurpleAccount *account = NULL;
PurpleBuddy *buddy = NULL;
const gchar *name = NULL;
account = purple_conversation_get_account(conversation);
name = purple_conversation_get_name(conversation);
buddy = purple_blist_find_buddy(account, name);
if(PURPLE_IS_BUDDY(buddy)) {
presence = purple_buddy_get_presence(buddy);
}
fallback = "person";
}
presence_icon = PIDGIN_PRESENCE_ICON(pane->presence_icon);
pidgin_presence_icon_set_fallback(presence_icon, fallback);
pidgin_presence_icon_set_presence(presence_icon, presence);
}
/* Notify that we changed. */
g_object_notify_by_pspec(G_OBJECT(pane), properties[PROP_CONVERSATION]);
}
/******************************************************************************
* GObject Implementation
*****************************************************************************/
static void
pidgin_info_pane_get_property(GObject *obj, guint param_id, GValue *value,
GParamSpec *pspec)
{
PidginInfoPane *pane = PIDGIN_INFO_PANE(obj);
switch(param_id) {
case PROP_CONVERSATION:
g_value_set_object(value, pidgin_info_pane_get_conversation(pane));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
break;
}
}
static void
pidgin_info_pane_set_property(GObject *obj, guint param_id, const GValue *value,
GParamSpec *pspec)
{
PidginInfoPane *pane = PIDGIN_INFO_PANE(obj);
switch(param_id) {
case PROP_CONVERSATION:
pidgin_info_pane_set_conversation(pane, g_value_get_object(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
break;
}
}
static void
pidgin_info_pane_dispose(GObject *obj) {
PidginInfoPane *pane = PIDGIN_INFO_PANE(obj);
g_clear_object(&pane->conversation);
G_OBJECT_CLASS(pidgin_info_pane_parent_class)->dispose(obj);
}
static void
pidgin_info_pane_init(PidginInfoPane *pane) {
gtk_widget_init_template(GTK_WIDGET(pane));
}
static void
pidgin_info_pane_class_init(PidginInfoPaneClass *klass) {
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
obj_class->get_property = pidgin_info_pane_get_property;
obj_class->set_property = pidgin_info_pane_set_property;
obj_class->dispose = pidgin_info_pane_dispose;
/**
* PidginInfoPane:conversation
*
* The #PurpleConversation whose information will be displayed.
*/
properties[PROP_CONVERSATION] = g_param_spec_object(
"conversation", "conversation",
"The PurpleConversation instance",
PURPLE_TYPE_CONVERSATION,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
gtk_widget_class_set_template_from_resource(
widget_class,
"/im/pidgin/Pidgin3/Conversations/infopane.ui"
);
gtk_widget_class_bind_template_child(widget_class, PidginInfoPane, hbox);
gtk_widget_class_bind_template_child(widget_class, PidginInfoPane, avatar);
gtk_widget_class_bind_template_child(widget_class, PidginInfoPane, presence_icon);
gtk_widget_class_bind_template_child(widget_class, PidginInfoPane, name);
gtk_widget_class_bind_template_child(widget_class, PidginInfoPane, topic);
}
/******************************************************************************
* API
*****************************************************************************/
GtkWidget *
pidgin_info_pane_new(PurpleConversation *conversation) {
return GTK_WIDGET(g_object_new(
PIDGIN_TYPE_INFO_PANE,
"conversation", conversation,
NULL));
}
PurpleConversation *
pidgin_info_pane_get_conversation(PidginInfoPane *pane) {
g_return_val_if_fail(PIDGIN_IS_INFO_PANE(pane), NULL);
return pane->conversation;
}