From 084de92f8c7109e89f0ccee6b7ecd463a787cfdd Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 24 Apr 2024 15:37:06 +0100 Subject: [PATCH] Fix copy/append of the internal USERDATA objects Issue: #301 Closes: #301 --- src/ucl_util.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/ucl_util.c b/src/ucl_util.c index 8f97c20..b00f277 100644 --- a/src/ucl_util.c +++ b/src/ucl_util.c @@ -3391,10 +3391,20 @@ ucl_elt_append (ucl_object_t *head, ucl_object_t *elt) head = elt; } else { - elt->prev = head->prev; - head->prev->next = elt; - head->prev = elt; - elt->next = NULL; + if (head->type == UCL_USERDATA) { + /* Userdata objects are VERY special! */ + struct ucl_object_userdata *ud = (struct ucl_object_userdata *)head; + elt->prev = ud->obj.prev; + ud->obj.prev->next = elt; + ud->obj.prev = elt; + elt->next = NULL; + } + else { + elt->prev = head->prev; + head->prev->next = elt; + head->prev = elt; + elt->next = NULL; + } } return head; @@ -3604,11 +3614,15 @@ ucl_object_copy_internal (const ucl_object_t *other, bool allow_array) ucl_object_t *new; ucl_object_iter_t it = NULL; const ucl_object_t *cur; + size_t sz = sizeof(*new); - new = malloc (sizeof (*new)); + if (other->type == UCL_USERDATA) { + sz = sizeof (struct ucl_object_userdata); + } + new = malloc (sz); if (new != NULL) { - memcpy (new, other, sizeof (*new)); + memcpy (new, other, sz); if (other->flags & UCL_OBJECT_EPHEMERAL) { /* Copied object is always non ephemeral */ new->flags &= ~UCL_OBJECT_EPHEMERAL;