1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-02-02 15:08:15 -05:00

Fixed some memory leaks in capabilities

This commit is contained in:
James Booth 2013-01-23 00:21:05 +00:00
parent 114d251b68
commit a48b48b787
4 changed files with 45 additions and 26 deletions

View File

@ -72,9 +72,16 @@ caps_get(const char * const caps_str)
char * char *
caps_get_sha1_str(xmpp_stanza_t *query) caps_get_sha1_str(xmpp_stanza_t *query)
{ {
char *category = NULL;
char *type = NULL;
char *lang = NULL;
char *name = NULL;
char *feature_str = NULL;
GSList *identities = NULL; GSList *identities = NULL;
GSList *features = NULL; GSList *features = NULL;
GSList *form_names = NULL; GSList *form_names = NULL;
DataForm *form = NULL;
FormField *field = NULL;
GHashTable *forms = g_hash_table_new(g_str_hash, g_str_equal); GHashTable *forms = g_hash_table_new(g_str_hash, g_str_equal);
GString *s = g_string_new(""); GString *s = g_string_new("");
@ -82,34 +89,40 @@ caps_get_sha1_str(xmpp_stanza_t *query)
xmpp_stanza_t *child = xmpp_stanza_get_children(query); xmpp_stanza_t *child = xmpp_stanza_get_children(query);
while (child != NULL) { while (child != NULL) {
if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_IDENTITY) == 0) { if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_IDENTITY) == 0) {
char *category = xmpp_stanza_get_attribute(child, "category"); category = g_strdup(xmpp_stanza_get_attribute(child, "category"));
char *type = xmpp_stanza_get_attribute(child, "type"); type = g_strdup(xmpp_stanza_get_attribute(child, "type"));
char *lang = xmpp_stanza_get_attribute(child, "xml:lang"); lang = g_strdup(xmpp_stanza_get_attribute(child, "xml:lang"));
char *name = xmpp_stanza_get_attribute(child, "name"); name = g_strdup(xmpp_stanza_get_attribute(child, "name"));
GString *identity_str = g_string_new(category); GString *identity_str = g_string_new(g_strdup(category));
g_string_append(identity_str, "/"); g_string_append(identity_str, "/");
if (type != NULL) { if (type != NULL) {
g_string_append(identity_str, type); g_string_append(identity_str, g_strdup(type));
} }
g_string_append(identity_str, "/"); g_string_append(identity_str, "/");
if (lang != NULL) { if (lang != NULL) {
g_string_append(identity_str, lang); g_string_append(identity_str, g_strdup(lang));
} }
g_string_append(identity_str, "/"); g_string_append(identity_str, "/");
if (name != NULL) { if (name != NULL) {
g_string_append(identity_str, name); g_string_append(identity_str, g_strdup(name));
} }
g_string_append(identity_str, "<"); g_string_append(identity_str, "<");
identities = g_slist_insert_sorted(identities, identity_str->str, (GCompareFunc)octet_compare); identities = g_slist_insert_sorted(identities, g_strdup(identity_str->str), (GCompareFunc)octet_compare);
GFREE_SET_NULL(category);
GFREE_SET_NULL(type);
GFREE_SET_NULL(lang);
GFREE_SET_NULL(name);
g_string_free(identity_str, TRUE);
} else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_FEATURE) == 0) { } else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_FEATURE) == 0) {
char *feature_str = xmpp_stanza_get_attribute(child, "var"); feature_str = g_strdup(xmpp_stanza_get_attribute(child, "var"));
features = g_slist_insert_sorted(features, feature_str, (GCompareFunc)octet_compare); features = g_slist_insert_sorted(features, g_strdup(feature_str), (GCompareFunc)octet_compare);
GFREE_SET_NULL(feature_str);
} else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_X) == 0) { } else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_X) == 0) {
if (strcmp(xmpp_stanza_get_ns(child), STANZA_NS_DATA) == 0) { if (strcmp(xmpp_stanza_get_ns(child), STANZA_NS_DATA) == 0) {
DataForm *form = stanza_get_form(child); form = stanza_create_form(child);
form_names = g_slist_insert_sorted(form_names, form->form_type, (GCompareFunc)octet_compare); form_names = g_slist_insert_sorted(form_names, strdup(form->form_type), (GCompareFunc)octet_compare);
g_hash_table_insert(forms, form->form_type, form); g_hash_table_insert(forms, strdup(form->form_type), form);
} }
} }
child = xmpp_stanza_get_next(child); child = xmpp_stanza_get_next(child);
@ -117,30 +130,30 @@ caps_get_sha1_str(xmpp_stanza_t *query)
GSList *curr = identities; GSList *curr = identities;
while (curr != NULL) { while (curr != NULL) {
g_string_append(s, curr->data); g_string_append(s, strdup(curr->data));
curr = g_slist_next(curr); curr = g_slist_next(curr);
} }
curr = features; curr = features;
while (curr != NULL) { while (curr != NULL) {
g_string_append(s, curr->data); g_string_append(s, strdup(curr->data));
g_string_append(s, "<"); g_string_append(s, "<");
curr = g_slist_next(curr); curr = g_slist_next(curr);
} }
curr = form_names; curr = form_names;
while (curr != NULL) { while (curr != NULL) {
DataForm *form = g_hash_table_lookup(forms, curr->data); form = g_hash_table_lookup(forms, curr->data);
g_string_append(s, form->form_type); g_string_append(s, strdup(form->form_type));
g_string_append(s, "<"); g_string_append(s, "<");
GSList *curr_field = form->fields; GSList *curr_field = form->fields;
while (curr_field != NULL) { while (curr_field != NULL) {
FormField *field = curr_field->data; field = curr_field->data;
g_string_append(s, field->var); g_string_append(s, strdup(field->var));
GSList *curr_value = field->values; GSList *curr_value = field->values;
while (curr_value != NULL) { while (curr_value != NULL) {
g_string_append(s, curr_value->data); g_string_append(s, strdup(curr_value->data));
g_string_append(s, "<"); g_string_append(s, "<");
curr_value = g_slist_next(curr_value); curr_value = g_slist_next(curr_value);
} }
@ -164,8 +177,8 @@ caps_get_sha1_str(xmpp_stanza_t *query)
char *result = g_base64_encode(md_value, md_len); char *result = g_base64_encode(md_value, md_len);
g_string_free(s, TRUE); g_string_free(s, TRUE);
g_slist_free(identities); g_slist_free_full(identities, free);
g_slist_free(features); g_slist_free_full(features, free);
return result; return result;
} }

View File

@ -44,6 +44,12 @@
} \ } \
} }
#define GFREE_SET_NULL(resource) \
{ \
g_free(resource); \
resource = NULL; \
}
void p_slist_free_full(GSList *items, GDestroyNotify free_func); void p_slist_free_full(GSList *items, GDestroyNotify free_func);
void create_dir(char *name); void create_dir(char *name);
char * str_replace(const char *string, const char *substr, char * str_replace(const char *string, const char *substr,

View File

@ -536,7 +536,7 @@ stanza_get_caps_str(xmpp_stanza_t * const stanza)
} }
DataForm * DataForm *
stanza_get_form(xmpp_stanza_t * const stanza) stanza_create_form(xmpp_stanza_t * const stanza)
{ {
DataForm *result = NULL; DataForm *result = NULL;
@ -568,7 +568,7 @@ stanza_get_form(xmpp_stanza_t * const stanza)
// handle values // handle values
while (value != NULL) { while (value != NULL) {
char *text = xmpp_stanza_get_text(value); char *text = xmpp_stanza_get_text(value);
field->values = g_slist_insert_sorted(field->values, text, (GCompareFunc)octet_compare); field->values = g_slist_insert_sorted(field->values, strdup(text), (GCompareFunc)octet_compare);
value = xmpp_stanza_get_next(value); value = xmpp_stanza_get_next(value);
} }

View File

@ -147,6 +147,6 @@ gboolean stanza_is_caps_request(xmpp_stanza_t * const stanza);
gboolean stanza_is_version_request(xmpp_stanza_t * const stanza); gboolean stanza_is_version_request(xmpp_stanza_t * const stanza);
DataForm * stanza_get_form(xmpp_stanza_t * const stanza); DataForm * stanza_create_form(xmpp_stanza_t * const stanza);
#endif #endif