handler: no need to dup string

`hash_iter_next()` returns a pointer to an element of the data structure
which is persistent and not altered while calling `hash_iter_next()`.
Therefor we don't have to dup the string, but simply can remember the
pointer.
In the case where the element would be `hash_drop()`'ped which contains
the key, the key is not accessed after it has been free'd.

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
This commit is contained in:
Steffen Jaeckel
2021-11-23 12:13:45 +01:00
parent 07a4f778d3
commit 01f2c3128a

View File

@@ -665,8 +665,7 @@ void handler_system_delete_all(xmpp_conn_t *conn)
{ {
xmpp_handlist_t *item, *next, *head, *head_old; xmpp_handlist_t *item, *next, *head, *head_old;
hash_iterator_t *iter; hash_iterator_t *iter;
const char *key; const char *key, *key2;
char *key2 = NULL;
/* TODO unify all kinds of handlers and avoid copy-paste below */ /* TODO unify all kinds of handlers and avoid copy-paste below */
@@ -714,7 +713,7 @@ void handler_system_delete_all(xmpp_conn_t *conn)
item = item->next; item = item->next;
} }
if (head != head_old) if (head != head_old)
key2 = xmpp_strdup(conn->ctx, key); key2 = key;
/* Hash table implementation is not perfect, so we need to find next /* Hash table implementation is not perfect, so we need to find next
key before dropping current one. Otherwise, we will get access to key before dropping current one. Otherwise, we will get access to
freed memory. */ freed memory. */
@@ -725,7 +724,6 @@ void handler_system_delete_all(xmpp_conn_t *conn)
hash_add(conn->id_handlers, key2, head); hash_add(conn->id_handlers, key2, head);
else else
hash_drop(conn->id_handlers, key2); hash_drop(conn->id_handlers, key2);
xmpp_free(conn->ctx, key2);
} }
} }
if (iter) if (iter)