From ab4adf19d0327008a7a302a7e0df40ab86afcf5e Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Sat, 28 Feb 2026 10:02:26 +0100 Subject: [PATCH] refactor: Refactor form_set_value to use glib list management Simplify how field values are updated and ensure memory is correctly managed. The previous implementation relied on manual manipulation of the GSList internal `data` member and only handled cases where a field had zero or one existing value. The function now correctly handles fields that may already contain multiple values by using 'g_slist_free_full' to clear the entire list and its contents before setting the new value. --- src/xmpp/form.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/xmpp/form.c b/src/xmpp/form.c index 8f9e0bc9..1b8f84f4 100644 --- a/src/xmpp/form.c +++ b/src/xmpp/form.c @@ -477,16 +477,13 @@ form_set_value(DataForm* form, const char* const tag, char* value) while (curr) { FormField* field = curr->data; if (g_strcmp0(field->var, var) == 0) { - if (g_slist_length(field->values) == 0) { - field->values = g_slist_append(field->values, strdup(value)); - form->modified = TRUE; - return; - } else if (g_slist_length(field->values) == 1) { - free(field->values->data); - field->values->data = strdup(value); - form->modified = TRUE; - return; + if (field->values) { + g_slist_free_full(field->values, free); + field->values = NULL; } + field->values = g_slist_append(NULL, strdup(value)); + form->modified = TRUE; + return; } curr = g_slist_next(curr); }