schema BUGFIX handle empty subtrees in lys_getnext()
correct moving through an empty multi-level subtree (of choices, cases
and non-presence containers).
diff --git a/src/tree_schema.c b/src/tree_schema.c
index f38ea93..45312ec 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -103,6 +103,22 @@
return LY_SUCCESS;
}
+static void
+lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
+{
+ for (; first_case; first_case = (const struct lysc_node_case*)first_case->next) {
+ if (first_case->child) {
+ /* there is something to return */
+ (*next) = first_case->child;
+ return;
+ }
+ }
+
+ /* no children in choice's cases, so go to the choice's sibling instead of into it */
+ (*last) = (*next);
+ (*next) = (*next)->next;
+}
+
API const struct lysc_node *
lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, uint32_t options)
{
@@ -124,7 +140,7 @@
/* schema subtree */
if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
if (((struct lysc_node_choice *)parent)->cases) {
- next = last = (const struct lysc_node *)&((struct lysc_node_choice *)parent)->cases[0];
+ next = last = (const struct lysc_node *)((struct lysc_node_choice *)parent)->cases;
}
} else {
snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
@@ -213,7 +229,7 @@
break;
} else {
/* go into */
- next = ((struct lysc_node_case *)next)->child;
+ lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
}
goto repeat;
case LYS_CONTAINER:
@@ -222,6 +238,7 @@
/* go into */
next = ((struct lysc_node_container *)next)->child;
} else {
+ last = next;
next = next->next;
}
goto repeat;
@@ -233,22 +250,11 @@
} else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
next = next->next;
} else {
- /* go into */
if (options & LYS_GETNEXT_WITHCASE) {
next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
} else {
- const struct lysc_node_case *cs = NULL;
- for (cs = ((struct lysc_node_choice *)next)->cases; cs; cs = (const struct lysc_node_case*)cs->next) {
- if (cs->child) {
- /* there is something to return */
- next = cs->child;
- break;
- }
- }
- if (!cs) {
- /* no children in choice's cases, so go to the choice's sibling instead of into it */
- next = next->next;
- }
+ /* go into */
+ lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
}
}
goto repeat;
diff --git a/tests/utests/schema/test_schema_common.c b/tests/utests/schema/test_schema_common.c
index c959a2e..a43d6fc 100644
--- a/tests/utests/schema/test_schema_common.c
+++ b/tests/utests/schema/test_schema_common.c
@@ -46,7 +46,7 @@
" anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
" notification nine {leaf nine-data {type string;}}}"
"leaf b {type string;} leaf-list c {type string;} list d {config false;}"
- "choice x { case empty-x; leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
+ "choice x { case empty-x { choice empty-xc { case nothing;}} leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
"rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
"rpc i;"
"notification j {leaf i-data {type string;}}"
@@ -142,6 +142,12 @@
assert_string_equal("d", node->name);
assert_null(node = lys_getnext(node, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK));
+ assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; container c {container cc;} leaf a {type string;}}", LYS_IN_YANG, &mod));
+ assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
+ assert_string_equal("c", node->name);
+ assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
+ assert_string_equal("a", node->name);
+
*state = NULL;
ly_ctx_destroy(ctx, NULL);
}