From b7d91607b64e66de56edc6fa73e1163a59b10ab2 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Thu, 16 Jan 2025 17:26:23 +0100 Subject: [PATCH] Fixed memory issue as result of optimization, modify xmldb_copy to reset rather than free --- lib/clixon/clixon_xml.h | 3 ++- lib/src/clixon_datastore.c | 5 +++-- lib/src/clixon_xml.c | 30 ++++++++++++++++++++++++------ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/lib/clixon/clixon_xml.h b/lib/clixon/clixon_xml.h index 876494c7..96c0e7d1 100644 --- a/lib/clixon/clixon_xml.h +++ b/lib/clixon/clixon_xml.h @@ -242,7 +242,7 @@ char *xml_value(cxobj *xn); int xml_value_set(cxobj *xn, char *val); int xml_value_append(cxobj *xn, char *val); enum cxobj_type xml_type(cxobj *xn); - +enum cxobj_type xml_type_set(cxobj *xn, enum cxobj_type type); int xml_child_nr(cxobj *xn); int xml_child_nr_type(cxobj *xn, enum cxobj_type type); int xml_child_nr_notype(cxobj *xn, enum cxobj_type type); @@ -285,6 +285,7 @@ cxobj *xml_find_type(cxobj *xn_parent, const char *prefix, const char *name, char *xml_find_value(cxobj *xn_parent, const char *name); char *xml_find_body(cxobj *xn, const char *name); cxobj *xml_find_body_obj(cxobj *xt, const char *name, char *val); +int xml_free0(cxobj *x); int xml_free(cxobj *xn); int xml_copy_one(cxobj *xn0, cxobj *xn1); int xml_copy(cxobj *x0, cxobj *x1); diff --git a/lib/src/clixon_datastore.c b/lib/src/clixon_datastore.c index f8c27056..2506c377 100644 --- a/lib/src/clixon_datastore.c +++ b/lib/src/clixon_datastore.c @@ -328,8 +328,9 @@ xmldb_copy(clixon_handle h, goto done; } else{ /* copy x1 to x2 */ - xml_free(x2); - if ((x2 = xml_new(xml_name(x1), NULL, CX_ELMNT)) == NULL) + xml_free0(x2); + xml_type_set(x2, CX_ELMNT); + if (xml_name_set(x2, xml_name(x1)) < 0) goto done; xml_flag_set(x2, XML_FLAG_TOP); if (xml_copy(x1, x2) < 0) diff --git a/lib/src/clixon_xml.c b/lib/src/clixon_xml.c index 908a6211..0c4b28ad 100644 --- a/lib/src/clixon_xml.c +++ b/lib/src/clixon_xml.c @@ -743,7 +743,7 @@ xml_type(cxobj *xn) * @param[in] type new type * @retval type old type */ -static enum cxobj_type +enum cxobj_type xml_type_set(cxobj *xn, enum cxobj_type type) { @@ -1945,26 +1945,28 @@ xml_find_body_obj(cxobj *xt, return x; } -/*! Free an xl sub-tree recursively, but do not remove it from parent +/*! Free an xml sub-tree recursively, reset it, but not object itself, do not remove it from parent * - * @param[in] x the xml tree to be freed. + * @param[in] x the xml tree to be reset * @see xml_purge where x is also removed from parent + * @see xml_free also free object */ int -xml_free(cxobj *x) +xml_free0(cxobj *x) { int i; cxobj *xc; + size_t sz; - if (x == NULL){ + if (x == NULL) return 0; - } if (x->x_name) free(x->x_name); if (x->x_prefix) free(x->x_prefix); switch (xml_type(x)){ case CX_ELMNT: + sz = sizeof(struct xml); for (i=0; ix_childvec_len; i++){ if ((xc = x->x_childvec[i]) != NULL){ xml_free(xc); @@ -1983,12 +1985,28 @@ xml_free(cxobj *x) break; case CX_BODY: case CX_ATTR: + sz = sizeof(struct xmlbody); if (x->x_value_cb) cbuf_free(x->x_value_cb); break; default: break; } + memset(x, 0, sz); + return 0; +} + +/*! Free an xml sub-tree recursively, but do not remove it from parent + * + * @param[in] x the xml tree to be freed. + * @see xml_purge where x is also removed from parent + */ +int +xml_free(cxobj *x) +{ + if (x == NULL) + return 0; + xml_free0(x); free(x); _stats_xml_nr--; return 0;