From 9b82701464c2e9c9e289032ec6cd9c61bec95fb7 Mon Sep 17 00:00:00 2001 From: Stefan Gula Date: Sun, 29 Oct 2023 16:16:42 +0100 Subject: [PATCH 1/4] Fixing segfault after invalid node --- src/path.c | 11 ++++++++++- tests/utests/types/leafref.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/path.c b/src/path.c index 516fe5720..9a5e9b34a 100644 --- a/src/path.c +++ b/src/path.c @@ -1068,7 +1068,17 @@ ly_path_compile_deref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node LY_CHECK_GOTO(ret = ly_path_compile_leafref(ctx, ctx_node, top_ext, &expr2, oper, target, format, prefix_data, &path2), cleanup); node2 = path2[LY_ARRAY_COUNT(path2) - 1].node; + if (node2->nodetype != LYS_LEAF && node2->nodetype != LYS_LEAFLIST) { + LOGVAL(ctx, LYVE_XPATH, "The deref function target node \"%s\" is not leaf nor leaflist", node2->name); + ret = LY_EVALID; + goto cleanup; + } deref_leaf_node = (const struct lysc_node_leaf *)node2; + if (deref_leaf_node->type->basetype != LY_TYPE_LEAFREF) { + LOGVAL(ctx, LYVE_XPATH, "The deref function target node \"%s\" is not having leafref type", node2->name); + ret = LY_EVALID; + goto cleanup; + } lref = (const struct lysc_type_leafref *)deref_leaf_node->type; LY_CHECK_GOTO(ret = ly_path_append(ctx, path2, path), cleanup); ly_path_free(ctx, path2); @@ -1172,7 +1182,6 @@ _ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, con (expr->tokens[tok_idx] == LYXP_TOKEN_FUNCNAME)) { /* deref function */ ret = ly_path_compile_deref(ctx, ctx_node, top_ext, expr, oper, target, format, prefix_data, &tok_idx, path); - ctx_node = (*path)[LY_ARRAY_COUNT(*path) - 1].node; goto cleanup; } else if (expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH) { /* absolute path */ diff --git a/tests/utests/types/leafref.c b/tests/utests/types/leafref.c index 7005d08bf..410dd07a1 100644 --- a/tests/utests/types/leafref.c +++ b/tests/utests/types/leafref.c @@ -241,6 +241,37 @@ test_data_xpath_json(void **state) lyd_free_all(tree); } +static void +test_xpath_invalid_schema(void **state) +{ + const char *schema1, *schema2; + ly_ctx_set_options(UTEST_LYCTX, LY_CTX_LEAFREF_EXTENDED); + schema1 = MODULE_CREATE_YANG("xp_test", + "list l1 {key t1;" + "leaf t1 {type uint8;}" + "list l2 {key t2;" + "leaf t2 {type uint8;}" + "leaf-list l3 {type uint8;}" + "}}" + "leaf r1 {type leafref {path \"deref(../l1)/../l2/t2\";}}"); + + UTEST_INVALID_MODULE(schema1, LYS_IN_YANG, NULL, LY_EVALID) + CHECK_LOG_CTX("The deref function target node \"l1\" is not leaf nor leaflist", "Schema location \"/xp_test:r1\"."); + + schema2 = MODULE_CREATE_YANG("xp_test", + "list l1 {key t1;" + "leaf t1 {type uint8;}" + "list l2 {key t2;" + "leaf t2 {type uint8;}" + "leaf-list l3 {type uint8;}" + "}}" + "leaf r1 {type uint8;}" + "leaf r2 {type leafref {path \"deref(../r1)/../l2/t2\";}}"); + + UTEST_INVALID_MODULE(schema2, LYS_IN_YANG, NULL, LY_EVALID) + CHECK_LOG_CTX("The deref function target node \"r1\" is not having leafref type", "Schema location \"/xp_test:r2\"."); +} + int main(void) { @@ -249,6 +280,7 @@ main(void) UTEST(test_data_json), UTEST(test_plugin_lyb), UTEST(test_data_xpath_json), + UTEST(test_xpath_invalid_schema) }; return cmocka_run_group_tests(tests, NULL, NULL); From 3a78675af129a76c93886c62a08d9ee70a1aa6c2 Mon Sep 17 00:00:00 2001 From: Stefan Gula Date: Sun, 29 Oct 2023 16:41:47 +0100 Subject: [PATCH 2/4] Fixing uncrustify error --- src/path.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/path.c b/src/path.c index 9a5e9b34a..d6e0354f8 100644 --- a/src/path.c +++ b/src/path.c @@ -1068,7 +1068,7 @@ ly_path_compile_deref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node LY_CHECK_GOTO(ret = ly_path_compile_leafref(ctx, ctx_node, top_ext, &expr2, oper, target, format, prefix_data, &path2), cleanup); node2 = path2[LY_ARRAY_COUNT(path2) - 1].node; - if (node2->nodetype != LYS_LEAF && node2->nodetype != LYS_LEAFLIST) { + if ((node2->nodetype != LYS_LEAF) && (node2->nodetype != LYS_LEAFLIST)) { LOGVAL(ctx, LYVE_XPATH, "The deref function target node \"%s\" is not leaf nor leaflist", node2->name); ret = LY_EVALID; goto cleanup; From 73a2725dcb41b0c18a4dc2fb135324aba1c733da Mon Sep 17 00:00:00 2001 From: Stefan Gula Date: Tue, 31 Oct 2023 12:47:41 +0100 Subject: [PATCH 3/4] Rebased to devel, fixed uncrustify error --- tests/utests/types/leafref.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/utests/types/leafref.c b/tests/utests/types/leafref.c index 410dd07a1..fcfe35153 100644 --- a/tests/utests/types/leafref.c +++ b/tests/utests/types/leafref.c @@ -245,6 +245,7 @@ static void test_xpath_invalid_schema(void **state) { const char *schema1, *schema2; + ly_ctx_set_options(UTEST_LYCTX, LY_CTX_LEAFREF_EXTENDED); schema1 = MODULE_CREATE_YANG("xp_test", "list l1 {key t1;" From c14c10c0c56f43018f4399c4b5750b05774da529 Mon Sep 17 00:00:00 2001 From: Stefan Gula Date: Tue, 31 Oct 2023 12:49:00 +0100 Subject: [PATCH 4/4] Fixed comments based on PR review --- src/path.c | 2 +- tests/utests/types/leafref.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/path.c b/src/path.c index d6e0354f8..73883d754 100644 --- a/src/path.c +++ b/src/path.c @@ -1075,7 +1075,7 @@ ly_path_compile_deref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node } deref_leaf_node = (const struct lysc_node_leaf *)node2; if (deref_leaf_node->type->basetype != LY_TYPE_LEAFREF) { - LOGVAL(ctx, LYVE_XPATH, "The deref function target node \"%s\" is not having leafref type", node2->name); + LOGVAL(ctx, LYVE_XPATH, "The deref function target node \"%s\" is not leafref", node2->name); ret = LY_EVALID; goto cleanup; } diff --git a/tests/utests/types/leafref.c b/tests/utests/types/leafref.c index fcfe35153..21e91cd70 100644 --- a/tests/utests/types/leafref.c +++ b/tests/utests/types/leafref.c @@ -270,7 +270,7 @@ test_xpath_invalid_schema(void **state) "leaf r2 {type leafref {path \"deref(../r1)/../l2/t2\";}}"); UTEST_INVALID_MODULE(schema2, LYS_IN_YANG, NULL, LY_EVALID) - CHECK_LOG_CTX("The deref function target node \"r1\" is not having leafref type", "Schema location \"/xp_test:r2\"."); + CHECK_LOG_CTX("The deref function target node \"r1\" is not leafref", "Schema location \"/xp_test:r2\"."); } int