From 21476c139954e3027f0967d446b3655f7a36a6ea Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Thu, 5 Dec 2024 15:44:01 +0100 Subject: [PATCH] Fixed: [Mem error when more multiple uses on top level with multiple statements in grouping](https://github.com/clicon/clixon/issues/583) --- CHANGELOG.md | 1 + lib/src/clixon_yang.c | 5 +---- lib/src/clixon_yang_parse_lib.c | 17 ++++++++++------- test/test_autocli_grouping.sh | 7 +++++++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5311f68b..21befc09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ Developers may need to change their code ### Corrected Bugs +* Fixed: [Mem error when more multiple uses on top level with multiple statements in grouping](https://github.com/clicon/clixon/issues/583) * Fixed: [Change CLICON_NETCONF_DUPLICATE_ALLOW to remove duplicates](https://github.com/clicon/clixon-controller/issues/160) * Fixed: Segv in canonical xpath transform * Fixed: [Error with submodules and feature Interaction](https://github.com/clicon/clixon-controller/issues/158) diff --git a/lib/src/clixon_yang.c b/lib/src/clixon_yang.c index 31609270..cb30c0d8 100644 --- a/lib/src/clixon_yang.c +++ b/lib/src/clixon_yang.c @@ -1148,21 +1148,18 @@ ys_prune_self(yang_stmt *ys) int retval = -1; yang_stmt *yp; yang_stmt *yc; - int i; int inext; if ((yp = yang_parent_get(ys)) != NULL){ - i = 0; inext = 0; /* Find order of ys in child-list */ while ((yc = yn_iter(yp, &inext)) != NULL) { if (ys == yc) break; - i++; } if (yc != NULL){ assert(yc == ys); - ys_prune(yp, i); + ys_prune(yp, inext-1); } } retval = 0; diff --git a/lib/src/clixon_yang_parse_lib.c b/lib/src/clixon_yang_parse_lib.c index 94a4d6f1..ee28ec56 100644 --- a/lib/src/clixon_yang_parse_lib.c +++ b/lib/src/clixon_yang_parse_lib.c @@ -569,9 +569,9 @@ ys_iskey(yang_stmt *y, /*! Helper function to yang_expand_grouping * * @param[in] h Clixon handle - * @param[in] yn Yang parent node of uses ststement + * @param[in] yn Yang parent node of uses statement * @param[in] ys Uses statement - * @param[in] i ys is i:th element of yn:s children + * @param[in] ysi ys is i:th element of yn:s children * @retval 0 OK * @retval -1 Error * @see yang_augment_node similar but for augment @@ -580,13 +580,14 @@ static int yang_expand_uses_node(clixon_handle h, yang_stmt *yn, yang_stmt *ys, - int i) + int ysi) { int retval = -1; char *id = NULL; char *prefix = NULL; yang_stmt *ygrouping; /* grouping original */ yang_stmt *ygrouping2; /* grouping copy */ + yang_stmt *ygp; /* parent of ygrouping */ yang_stmt *yg; /* grouping child */ yang_stmt *yr; /* refinement */ yang_stmt *yp; @@ -601,6 +602,7 @@ yang_expand_uses_node(clixon_handle h, /* Split argument into prefix and name */ if (nodeid_split(yang_argument_get(ys), &prefix, &id) < 0) goto done; + ygrouping = NULL; if (ys_grouping_resolve(ys, prefix, id, &ygrouping) < 0) goto done; if (ygrouping == NULL){ @@ -675,6 +677,7 @@ yang_expand_uses_node(clixon_handle h, if (yang_schemanode(yg) || yang_keyword_get(yg) == Y_UNKNOWN) glen++; } + ygp = yang_parent_get(ygrouping); /* * yn is parent: the children of ygrouping replaces ys. * Is there a case when glen == 0? YES AND THIS BREAKS @@ -682,7 +685,7 @@ yang_expand_uses_node(clixon_handle h, if (glen > 0){ int oldbuflen = yn->ys_len; /* size of existing elements up from i+1 (not uses-stmt) */ - size = (yang_len_get(yn) - i - 1)*sizeof(struct yang_stmt *); + size = (yang_len_get(yn) - ysi - 1)*sizeof(struct yang_stmt *); yn->ys_len += glen; if ((yn->ys_stmt = realloc(yn->ys_stmt, (yang_len_get(yn))*sizeof(yang_stmt *))) == 0){ clixon_err(OE_YANG, errno, "realloc"); @@ -694,13 +697,13 @@ yang_expand_uses_node(clixon_handle h, memset(&yn->ys_stmt[oldbuflen], 0, glen*sizeof(yang_stmt *)); /* Move existing elements if any */ if (size) - memmove(&yn->ys_stmt[i+glen+1], &yn->ys_stmt[i+1], size); + memmove(&yn->ys_stmt[ysi+glen+1], &yn->ys_stmt[ysi+1], size); } /* Note: yang_desc_schema_nodeid() requires ygrouping2 to be in yspec tree, * due to correct module prefixes etc. * cannot be dangling, insert into tree here and then prune immediately after while loop */ - if (yn_insert(yang_parent_get(ygrouping), ygrouping2) < 0) + if (yn_insert(ygp, ygrouping2) < 0) goto done; /* Iterate through refinements and modify grouping copy * See RFC 7950 7.13.2 yrt is the refine target node @@ -761,7 +764,7 @@ yang_expand_uses_node(clixon_handle h, /* This is for extensions that allow list keys to be optional, see restconf_main_extension_cb */ if (yang_flag_get(ys, YANG_FLAG_NOKEY)) yang_flag_set(yg, YANG_FLAG_NOKEY); - yn->ys_stmt[i+k+1] = yg; + yn->ys_stmt[ysi+k+1] = yg; yg->ys_parent = yn; yang_flag_set(yg, YANG_FLAG_GROUPING); k++; diff --git a/test/test_autocli_grouping.sh b/test/test_autocli_grouping.sh index 0841d3ac..95ad8385 100755 --- a/test/test_autocli_grouping.sh +++ b/test/test_autocli_grouping.sh @@ -101,6 +101,12 @@ module example { */ } } + grouping pg4 { + leaf value4{ + description "a value"; + type string; + } + } container table{ list parameter{ key name; @@ -116,6 +122,7 @@ module example { } } uses pg1; + uses pg4; } EOF