diff --git a/src/schema_compile.c b/src/schema_compile.c index 492fe921e..9140273ac 100644 --- a/src/schema_compile.c +++ b/src/schema_compile.c @@ -1382,10 +1382,9 @@ lys_compile_unres_depset_implement(struct ly_ctx *ctx, struct lys_glob_unres *un static LY_ERR lys_compile_unres_check_disabled(const struct lysc_node *node) { - const struct lysc_node *parent; + const struct lysc_node *parent, *iter; struct lysc_node_list *slist; LYA_COUNT_T u, v; - int found; if (node->flags & LYS_KEY) { LOG_LOCSET(node); @@ -1399,44 +1398,38 @@ lys_compile_unres_check_disabled(const struct lysc_node *node) continue; } - /* check and fix list uniques */ + /* remove every unique leaf that is the node or in its subtree, all are freed with it */ slist = (struct lysc_node_list *)parent; - found = 0; - LYA_FOR(slist->uniques, u) { - LYA_FOR(slist->uniques[u], v) { - if (slist->uniques[u][v] == (struct lysc_node_leaf *)node) { - found = 1; - break; + u = 0; + while (u < LYA_COUNT(slist->uniques)) { + v = 0; + while (v < LYA_COUNT(slist->uniques[u])) { + for (iter = &slist->uniques[u][v]->node; iter && (iter != node); iter = iter->parent) {} + if (!iter) { + ++v; + continue; } - } - - if (found) { - break; - } - } - if (found) { - if (LYA_COUNT(slist->uniques[u]) > 1) { /* remove the item */ if (v < LYA_COUNT(slist->uniques[u]) - 1) { memmove(&slist->uniques[u][v], &slist->uniques[u][v + 1], (LYA_COUNT(slist->uniques[u]) - v - 1) * sizeof slist->uniques[u][v]); } LYA_DECREMENT(slist->uniques[u]); - } else { - /* remove the whole unique array */ - LYA_FREE(slist->uniques[u]); - if (LYA_COUNT(slist->uniques) > 1) { - if (u < LYA_COUNT(slist->uniques) - 1) { - memmove(&slist->uniques[u], &slist->uniques[u + 1], - (LYA_COUNT(slist->uniques) - u - 1) * sizeof slist->uniques[u]); - } - LYA_DECREMENT(slist->uniques); - } else { - LYA_FREE(slist->uniques); - slist->uniques = NULL; - } } + + if (LYA_COUNT(slist->uniques[u])) { + ++u; + continue; + } + + /* remove the whole unique array */ + LYA_FREE(slist->uniques[u]); + if (u < LYA_COUNT(slist->uniques) - 1) { + memmove(&slist->uniques[u], &slist->uniques[u + 1], + (LYA_COUNT(slist->uniques) - u - 1) * sizeof slist->uniques[u]); + } + LYA_DECREMENT_FREE(slist->uniques); } } diff --git a/tests/utests/schema/test_tree_schema_compile.c b/tests/utests/schema/test_tree_schema_compile.c index 252f39cdd..cae0614d9 100644 --- a/tests/utests/schema/test_tree_schema_compile.c +++ b/tests/utests/schema/test_tree_schema_compile.c @@ -413,6 +413,16 @@ test_node_list(void **state) assert_string_equal("a", child->name); assert_false(child->flags & LYS_KEY); + /* unique leaves are removed with their disabled ancestor */ + assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, "module e {yang-version 1.1;namespace urn:e;prefix e;feature f;" + "list l {key k; unique \"c/x\"; unique \"c/y k\"; leaf k {type string;}" + "container c {if-feature f; leaf x {type string;} leaf y {type string;}}}}", LYS_IN_YANG, &mod)); + list = (struct lysc_node_list *)mod->compiled->data; + assert_non_null(list); + assert_int_equal(1, LYA_COUNT(list->uniques)); + assert_int_equal(1, LYA_COUNT(list->uniques[0])); + assert_string_equal("k", list->uniques[0][0]->name); + /* invalid */ assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, "module aa {namespace urn:aa;prefix aa;list l;}", LYS_IN_YANG, NULL)); CHECK_LOG_CTX("Missing key in list representing configuration data.", "/aa:l", 0);