From 01f2c3128a979fa43ae151fff8e685d3b326a09e Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 23 Nov 2021 12:13:45 +0100 Subject: [PATCH] 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 --- src/handler.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/handler.c b/src/handler.c index 16df683..b9af2c9 100644 --- a/src/handler.c +++ b/src/handler.c @@ -665,8 +665,7 @@ void handler_system_delete_all(xmpp_conn_t *conn) { xmpp_handlist_t *item, *next, *head, *head_old; hash_iterator_t *iter; - const char *key; - char *key2 = NULL; + const char *key, *key2; /* 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; } if (head != head_old) - key2 = xmpp_strdup(conn->ctx, key); + key2 = key; /* Hash table implementation is not perfect, so we need to find next key before dropping current one. Otherwise, we will get access to freed memory. */ @@ -725,7 +724,6 @@ void handler_system_delete_all(xmpp_conn_t *conn) hash_add(conn->id_handlers, key2, head); else hash_drop(conn->id_handlers, key2); - xmpp_free(conn->ctx, key2); } } if (iter)