From a48b48b787fa1494e33703ed72a926100d6ee975 Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 23 Jan 2013 00:21:05 +0000 Subject: Fixed some memory leaks in capabilities --- src/capabilities.c | 59 +++++++++++++++++++++++++++++++++--------------------- src/common.h | 6 ++++++ src/stanza.c | 4 ++-- src/stanza.h | 2 +- 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/capabilities.c b/src/capabilities.c index 56ca93ea..f2c9cf9a 100644 --- a/src/capabilities.c +++ b/src/capabilities.c @@ -72,9 +72,16 @@ caps_get(const char * const caps_str) char * 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 *features = NULL; GSList *form_names = NULL; + DataForm *form = NULL; + FormField *field = NULL; GHashTable *forms = g_hash_table_new(g_str_hash, g_str_equal); 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); while (child != NULL) { if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_IDENTITY) == 0) { - char *category = xmpp_stanza_get_attribute(child, "category"); - char *type = xmpp_stanza_get_attribute(child, "type"); - char *lang = xmpp_stanza_get_attribute(child, "xml:lang"); - char *name = xmpp_stanza_get_attribute(child, "name"); + category = g_strdup(xmpp_stanza_get_attribute(child, "category")); + type = g_strdup(xmpp_stanza_get_attribute(child, "type")); + lang = g_strdup(xmpp_stanza_get_attribute(child, "xml:lang")); + 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, "/"); if (type != NULL) { - g_string_append(identity_str, type); + g_string_append(identity_str, g_strdup(type)); } g_string_append(identity_str, "/"); if (lang != NULL) { - g_string_append(identity_str, lang); + g_string_append(identity_str, g_strdup(lang)); } g_string_append(identity_str, "/"); if (name != NULL) { - g_string_append(identity_str, name); + g_string_append(identity_str, g_strdup(name)); } 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) { - char *feature_str = xmpp_stanza_get_attribute(child, "var"); - features = g_slist_insert_sorted(features, feature_str, (GCompareFunc)octet_compare); + feature_str = g_strdup(xmpp_stanza_get_attribute(child, "var")); + 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) { if (strcmp(xmpp_stanza_get_ns(child), STANZA_NS_DATA) == 0) { - DataForm *form = stanza_get_form(child); - form_names = g_slist_insert_sorted(form_names, form->form_type, (GCompareFunc)octet_compare); - g_hash_table_insert(forms, form->form_type, form); + form = stanza_create_form(child); + form_names = g_slist_insert_sorted(form_names, strdup(form->form_type), (GCompareFunc)octet_compare); + g_hash_table_insert(forms, strdup(form->form_type), form); } } child = xmpp_stanza_get_next(child); @@ -117,30 +130,30 @@ caps_get_sha1_str(xmpp_stanza_t *query) GSList *curr = identities; while (curr != NULL) { - g_string_append(s, curr->data); + g_string_append(s, strdup(curr->data)); curr = g_slist_next(curr); } curr = features; while (curr != NULL) { - g_string_append(s, curr->data); + g_string_append(s, strdup(curr->data)); g_string_append(s, "<"); curr = g_slist_next(curr); } curr = form_names; while (curr != NULL) { - DataForm *form = g_hash_table_lookup(forms, curr->data); - g_string_append(s, form->form_type); + form = g_hash_table_lookup(forms, curr->data); + g_string_append(s, strdup(form->form_type)); g_string_append(s, "<"); GSList *curr_field = form->fields; while (curr_field != NULL) { - FormField *field = curr_field->data; - g_string_append(s, field->var); + field = curr_field->data; + g_string_append(s, strdup(field->var)); GSList *curr_value = field->values; while (curr_value != NULL) { - g_string_append(s, curr_value->data); + g_string_append(s, strdup(curr_value->data)); g_string_append(s, "<"); 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); g_string_free(s, TRUE); - g_slist_free(identities); - g_slist_free(features); + g_slist_free_full(identities, free); + g_slist_free_full(features, free); return result; } diff --git a/src/common.h b/src/common.h index 5c615256..419b290f 100644 --- a/src/common.h +++ b/src/common.h @@ -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 create_dir(char *name); char * str_replace(const char *string, const char *substr, diff --git a/src/stanza.c b/src/stanza.c index 5e90cf90..012896ff 100644 --- a/src/stanza.c +++ b/src/stanza.c @@ -536,7 +536,7 @@ stanza_get_caps_str(xmpp_stanza_t * const stanza) } DataForm * -stanza_get_form(xmpp_stanza_t * const stanza) +stanza_create_form(xmpp_stanza_t * const stanza) { DataForm *result = NULL; @@ -568,7 +568,7 @@ stanza_get_form(xmpp_stanza_t * const stanza) // handle values while (value != NULL) { 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); } diff --git a/src/stanza.h b/src/stanza.h index 497a7388..2c52e9fe 100644 --- a/src/stanza.h +++ b/src/stanza.h @@ -147,6 +147,6 @@ gboolean stanza_is_caps_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 -- cgit 1.4.1-2-gfad0