Fixed memory issue as result of optimization, modify xmldb_copy to reset rather than free

This commit is contained in:
Olof hagsand 2025-01-16 17:26:23 +01:00
parent fa62eb0872
commit b7d91607b6
3 changed files with 29 additions and 9 deletions

View file

@ -242,7 +242,7 @@ char *xml_value(cxobj *xn);
int xml_value_set(cxobj *xn, char *val); int xml_value_set(cxobj *xn, char *val);
int xml_value_append(cxobj *xn, char *val); int xml_value_append(cxobj *xn, char *val);
enum cxobj_type xml_type(cxobj *xn); 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(cxobj *xn);
int xml_child_nr_type(cxobj *xn, enum cxobj_type type); int xml_child_nr_type(cxobj *xn, enum cxobj_type type);
int xml_child_nr_notype(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_value(cxobj *xn_parent, const char *name);
char *xml_find_body(cxobj *xn, const char *name); char *xml_find_body(cxobj *xn, const char *name);
cxobj *xml_find_body_obj(cxobj *xt, const char *name, char *val); cxobj *xml_find_body_obj(cxobj *xt, const char *name, char *val);
int xml_free0(cxobj *x);
int xml_free(cxobj *xn); int xml_free(cxobj *xn);
int xml_copy_one(cxobj *xn0, cxobj *xn1); int xml_copy_one(cxobj *xn0, cxobj *xn1);
int xml_copy(cxobj *x0, cxobj *x1); int xml_copy(cxobj *x0, cxobj *x1);

View file

@ -328,8 +328,9 @@ xmldb_copy(clixon_handle h,
goto done; goto done;
} }
else{ /* copy x1 to x2 */ else{ /* copy x1 to x2 */
xml_free(x2); xml_free0(x2);
if ((x2 = xml_new(xml_name(x1), NULL, CX_ELMNT)) == NULL) xml_type_set(x2, CX_ELMNT);
if (xml_name_set(x2, xml_name(x1)) < 0)
goto done; goto done;
xml_flag_set(x2, XML_FLAG_TOP); xml_flag_set(x2, XML_FLAG_TOP);
if (xml_copy(x1, x2) < 0) if (xml_copy(x1, x2) < 0)

View file

@ -743,7 +743,7 @@ xml_type(cxobj *xn)
* @param[in] type new type * @param[in] type new type
* @retval type old type * @retval type old type
*/ */
static enum cxobj_type enum cxobj_type
xml_type_set(cxobj *xn, xml_type_set(cxobj *xn,
enum cxobj_type type) enum cxobj_type type)
{ {
@ -1945,26 +1945,28 @@ xml_find_body_obj(cxobj *xt,
return x; 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_purge where x is also removed from parent
* @see xml_free also free object
*/ */
int int
xml_free(cxobj *x) xml_free0(cxobj *x)
{ {
int i; int i;
cxobj *xc; cxobj *xc;
size_t sz;
if (x == NULL){ if (x == NULL)
return 0; return 0;
}
if (x->x_name) if (x->x_name)
free(x->x_name); free(x->x_name);
if (x->x_prefix) if (x->x_prefix)
free(x->x_prefix); free(x->x_prefix);
switch (xml_type(x)){ switch (xml_type(x)){
case CX_ELMNT: case CX_ELMNT:
sz = sizeof(struct xml);
for (i=0; i<x->x_childvec_len; i++){ for (i=0; i<x->x_childvec_len; i++){
if ((xc = x->x_childvec[i]) != NULL){ if ((xc = x->x_childvec[i]) != NULL){
xml_free(xc); xml_free(xc);
@ -1983,12 +1985,28 @@ xml_free(cxobj *x)
break; break;
case CX_BODY: case CX_BODY:
case CX_ATTR: case CX_ATTR:
sz = sizeof(struct xmlbody);
if (x->x_value_cb) if (x->x_value_cb)
cbuf_free(x->x_value_cb); cbuf_free(x->x_value_cb);
break; break;
default: default:
break; 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); free(x);
_stats_xml_nr--; _stats_xml_nr--;
return 0; return 0;