From 9233a2ed14d4e2cf36aadf130ec171e3adcee18f Mon Sep 17 00:00:00 2001 From: Eric Parsonage Date: Sun, 1 Nov 2026 22:03:33 +1030 Subject: [PATCH] xpath BUGFIX value functions on nodes without schema enum-value(), bit-is-set() and deref() read the schema of the first argument node without checking it, so they crashed on the opaque dummy node used for the when of a missing node and on the root node. Such a node has no value, so enum-value() returns NaN, bit-is-set() false and deref() an empty node-set, as derived-from() already does. Fixes #2604 --- src/xpath.c | 11 ++-- tests/utests/basic/test_xpath.c | 94 +++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 5 deletions(-) diff --git a/src/xpath.c b/src/xpath.c index 3ff1da40be..5f6c2752be 100644 --- a/src/xpath.c +++ b/src/xpath.c @@ -3704,7 +3704,8 @@ xpath_bit_is_set(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp LY_CHECK_RET(rc); set_fill_boolean(set, 0); - if (args[0]->used && (args[0]->val.nodes[0].node->schema->nodetype & LYD_NODE_TERM)) { + if (args[0]->used && (args[0]->val.nodes[0].type == LYXP_NODE_ELEM) && args[0]->val.nodes[0].node->schema && + (args[0]->val.nodes[0].node->schema->nodetype & LYD_NODE_TERM)) { leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; val = &leaf->value; if (val->realtype->basetype == LY_TYPE_UNION) { @@ -4123,10 +4124,10 @@ xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set /* clear */ lyxp_set_free_content(set); - if (args[0]->used) { + if (args[0]->used && (args[0]->val.nodes[0].type == LYXP_NODE_ELEM)) { leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; sleaf = (struct lysc_node_leaf *)leaf->schema; - if (sleaf->nodetype & LYD_NODE_TERM) { + if (sleaf && (sleaf->nodetype & LYD_NODE_TERM)) { ret = xpath_deref_type(&leaf->node, &leaf->value, sleaf->type, set); } } @@ -4343,10 +4344,10 @@ xpath_enum_value(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp } set_fill_number(set, NAN); - if (args[0]->used) { + if (args[0]->used && (args[0]->val.nodes[0].type == LYXP_NODE_ELEM)) { leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; sleaf = (struct lysc_node_leaf *)leaf->schema; - if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { + if (sleaf && (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { set_fill_number(set, leaf->value.enum_item->value); } } diff --git a/tests/utests/basic/test_xpath.c b/tests/utests/basic/test_xpath.c index 79ba4445cc..8fac8c914c 100644 --- a/tests/utests/basic/test_xpath.c +++ b/tests/utests/basic/test_xpath.c @@ -1326,6 +1326,99 @@ test_number(void **state) lyd_free_siblings(tree); } +static void +test_no_value(void **state) +{ + const char *schema_c = + "module c {\n" + " namespace urn:tests:c;\n" + " prefix c;\n" + " yang-version 1.1;\n" + "\n" + " identity base;\n" + " identity one {\n" + " base base;\n" + " }\n" + "\n" + " leaf t {\n" + " type string;\n" + " }\n" + " container ce {\n" + " presence \"test\";\n" + " leaf v {\n" + " when \"string(enum-value(.)) = 'NaN'\";\n" + " type enumeration {\n" + " enum x;\n" + " }\n" + " mandatory true;\n" + " }\n" + " }\n" + " container cb {\n" + " presence \"test\";\n" + " leaf v {\n" + " when \"not(bit-is-set(., 'f'))\";\n" + " type bits {\n" + " bit f;\n" + " }\n" + " mandatory true;\n" + " }\n" + " }\n" + " container cd {\n" + " presence \"test\";\n" + " leaf v {\n" + " when \"not(deref(.))\";\n" + " type leafref {\n" + " path \"/c:t\";\n" + " }\n" + " mandatory true;\n" + " }\n" + " }\n" + " container ci {\n" + " presence \"test\";\n" + " leaf v {\n" + " when \"not(derived-from-or-self(., 'c:one'))\";\n" + " type identityref {\n" + " base base;\n" + " }\n" + " mandatory true;\n" + " }\n" + " }\n" + "}"; + struct lyd_node *tree; + ly_bool r; + + UTEST_ADD_MODULE(schema_c, LYS_IN_YANG, NULL, NULL); + + /* the when of a missing node is evaluated on an opaque dummy node without a value */ + CHECK_PARSE_LYD_PARAM("", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); + CHECK_LOG_CTX("Mandatory node \"v\" instance does not exist.", "/c:ce", 0); + + CHECK_PARSE_LYD_PARAM("", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); + CHECK_LOG_CTX("Mandatory node \"v\" instance does not exist.", "/c:cb", 0); + + CHECK_PARSE_LYD_PARAM("", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); + CHECK_LOG_CTX("Mandatory node \"v\" instance does not exist.", "/c:cd", 0); + + CHECK_PARSE_LYD_PARAM("", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); + CHECK_LOG_CTX("Mandatory node \"v\" instance does not exist.", "/c:ci", 0); + + /* the root node has no value either */ + assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, "val", LYD_XML, + LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); + assert_non_null(tree); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "string(enum-value(/)) = 'NaN'", &r)); + assert_int_equal(r, 1); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "bit-is-set(/, 'f')", &r)); + assert_int_equal(r, 0); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "deref(/)", &r)); + assert_int_equal(r, 0); + + lyd_free_all(tree); +} + int main(void) { @@ -1346,6 +1439,7 @@ main(void) UTEST(test_mod, setup), UTEST(test_anydata, setup), UTEST(test_number, setup), + UTEST(test_no_value, setup), }; return cmocka_run_group_tests(tests, NULL, NULL);