From e2e805dd4fbfeadddc79d9b4063cfd04e341a7c4 Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 23 Jan 2013 22:24:54 +0000 Subject: Tidied up memory allocation in capabilities --- src/capabilities.c | 29 ++++++++++++++++------------- src/capabilities.h | 4 ++-- src/jabber.c | 8 ++++---- src/stanza.c | 20 ++++++++++++++++++++ src/stanza.h | 1 + 5 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/capabilities.c b/src/capabilities.c index f2c9cf9a..ec146554 100644 --- a/src/capabilities.c +++ b/src/capabilities.c @@ -70,7 +70,7 @@ caps_get(const char * const caps_str) } char * -caps_get_sha1_str(xmpp_stanza_t *query) +caps_create_sha1_str(xmpp_stanza_t * const query) { char *category = NULL; char *type = NULL; @@ -82,17 +82,17 @@ caps_get_sha1_str(xmpp_stanza_t *query) 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_full(g_str_hash, g_str_equal, free, (GDestroyNotify)stanza_destroy_form); GString *s = g_string_new(""); xmpp_stanza_t *child = xmpp_stanza_get_children(query); while (child != NULL) { if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_IDENTITY) == 0) { - 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")); + category = xmpp_stanza_get_attribute(child, "category"); + type = xmpp_stanza_get_attribute(child, "type"); + lang = xmpp_stanza_get_attribute(child, "xml:lang"); + name = xmpp_stanza_get_attribute(child, "name"); GString *identity_str = g_string_new(g_strdup(category)); g_string_append(identity_str, "/"); @@ -109,15 +109,10 @@ caps_get_sha1_str(xmpp_stanza_t *query) } g_string_append(identity_str, "<"); 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) { - feature_str = g_strdup(xmpp_stanza_get_attribute(child, "var")); + feature_str = 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) { form = stanza_create_form(child); @@ -179,12 +174,14 @@ caps_get_sha1_str(xmpp_stanza_t *query) g_string_free(s, TRUE); g_slist_free_full(identities, free); g_slist_free_full(features, free); + g_slist_free_full(form_names, free); + g_hash_table_destroy(forms); return result; } xmpp_stanza_t * -caps_get_query_response_stanza(xmpp_ctx_t *ctx) +caps_create_query_response_stanza(xmpp_ctx_t * const ctx) { xmpp_stanza_t *query = xmpp_stanza_new(ctx); xmpp_stanza_set_name(query, STANZA_NAME_QUERY); @@ -224,6 +221,12 @@ caps_get_query_response_stanza(xmpp_ctx_t *ctx) xmpp_stanza_add_child(query, feature_caps); xmpp_stanza_add_child(query, feature_version); + xmpp_stanza_release(identity); + xmpp_stanza_release(feature_muc); + xmpp_stanza_release(feature_discoinfo); + xmpp_stanza_release(feature_caps); + xmpp_stanza_release(feature_version); + return query; } diff --git a/src/capabilities.h b/src/capabilities.h index ad160826..59b63730 100644 --- a/src/capabilities.h +++ b/src/capabilities.h @@ -34,8 +34,8 @@ void caps_init(void); void caps_add(const char * const caps_str, const char * const client); gboolean caps_contains(const char * const caps_str); Capabilities* caps_get(const char * const caps_str); -char* caps_get_sha1_str(xmpp_stanza_t * const query); -xmpp_stanza_t* caps_get_query_response_stanza(xmpp_ctx_t *ctx); +char* caps_create_sha1_str(xmpp_stanza_t * const query); +xmpp_stanza_t* caps_create_query_response_stanza(xmpp_ctx_t * const ctx); void caps_close(void); #endif diff --git a/src/jabber.c b/src/jabber.c index 244921bf..58895764 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -428,8 +428,8 @@ jabber_update_presence(jabber_presence_t status, const char * const msg, xmpp_stanza_set_ns(caps, STANZA_NS_CAPS); xmpp_stanza_set_attribute(caps, STANZA_ATTR_HASH, "sha-1"); xmpp_stanza_set_attribute(caps, STANZA_ATTR_NODE, "http://www.profanity.im"); - xmpp_stanza_t *query = caps_get_query_response_stanza(jabber_conn.ctx); - char *sha1 = caps_get_sha1_str(query); + xmpp_stanza_t *query = caps_create_query_response_stanza(jabber_conn.ctx); + char *sha1 = caps_create_sha1_str(query); xmpp_stanza_set_attribute(caps, STANZA_ATTR_VER, sha1); xmpp_stanza_add_child(presence, caps); xmpp_send(jabber_conn.conn, presence); @@ -1015,7 +1015,7 @@ _disco_request_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, xmpp_stanza_set_id(response, xmpp_stanza_get_id(stanza)); xmpp_stanza_set_attribute(response, STANZA_ATTR_TO, from); xmpp_stanza_set_type(response, STANZA_TYPE_RESULT); - xmpp_stanza_t *query = caps_get_query_response_stanza(ctx); + xmpp_stanza_t *query = caps_create_query_response_stanza(ctx); xmpp_stanza_set_attribute(query, STANZA_ATTR_NODE, node_str); xmpp_stanza_add_child(response, query); xmpp_send(conn, response); @@ -1050,7 +1050,7 @@ _disco_response_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, // validate sha1 gchar **split = g_strsplit(node, "#", -1); char *given_sha1 = split[1]; - char *generated_sha1 = caps_get_sha1_str(query); + char *generated_sha1 = caps_create_sha1_str(query); if (g_strcmp0(given_sha1, generated_sha1) != 0) { log_info("Invalid SHA1 recieved for caps."); diff --git a/src/stanza.c b/src/stanza.c index 012896ff..554dfc1c 100644 --- a/src/stanza.c +++ b/src/stanza.c @@ -581,6 +581,26 @@ stanza_create_form(xmpp_stanza_t * const stanza) return result; } +void +stanza_destroy_form(DataForm *form) +{ + if (form != NULL) { + FREE_SET_NULL(form->form_type); + if (form->fields != NULL) { + GSList *curr_field = form->fields; + while (curr_field != NULL) { + FormField *field = curr_field->data; + FREE_SET_NULL(field->var); + if ((field->values) != NULL) { + g_slist_free_full(field->values, free); + } + } + } + + form = NULL; + } +} + static int _field_compare(FormField *f1, FormField *f2) { diff --git a/src/stanza.h b/src/stanza.h index 2c52e9fe..e9bb870f 100644 --- a/src/stanza.h +++ b/src/stanza.h @@ -148,5 +148,6 @@ gboolean stanza_is_caps_request(xmpp_stanza_t * const stanza); gboolean stanza_is_version_request(xmpp_stanza_t * const stanza); DataForm * stanza_create_form(xmpp_stanza_t * const stanza); +void stanza_destroy_form(DataForm *form); #endif -- cgit 1.4.1-2-gfad0