mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-18 13:26:22 +00:00
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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user