From 28a66bd73efdbcd494fd3f47b9c42aad9a5d5f08 Mon Sep 17 00:00:00 2001 From: Eric Parsonage Date: Sat, 5 Dec 2026 15:23:57 +1030 Subject: [PATCH] xpath BUGFIX node-set compared with a boolean A node-set compared with a boolean was compared node by node, so an empty node-set gave false whatever the boolean and the operator. XPath 1.0 section 3.4 converts the whole node-set with boolean() first, so an absent node compared with false() is equal. Fixes #2668 --- src/xpath.c | 10 ++++++--- tests/utests/basic/test_xpath.c | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/xpath.c b/src/xpath.c index b44784dc6..7913e2a2b 100644 --- a/src/xpath.c +++ b/src/xpath.c @@ -8091,9 +8091,6 @@ moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set case LYXP_SET_NUMBER: rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1); break; - case LYXP_SET_BOOLEAN: - rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1); - break; default: rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1); break; @@ -8235,6 +8232,13 @@ moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_ int cmp; LY_ERR rc; + /* a node-set compared with a boolean is converted to a boolean as a whole (XPath 1.0 section 3.4) */ + if ((set1->type == LYXP_SET_NODE_SET) && (set2->type == LYXP_SET_BOOLEAN)) { + LY_CHECK_RET(lyxp_set_cast(set1, LYXP_SET_BOOLEAN)); + } else if ((set1->type == LYXP_SET_BOOLEAN) && (set2->type == LYXP_SET_NODE_SET)) { + LY_CHECK_RET(lyxp_set_cast(set2, LYXP_SET_BOOLEAN)); + } + /* iterative evaluation with node-sets */ if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { if (set1->type == LYXP_SET_NODE_SET) { diff --git a/tests/utests/basic/test_xpath.c b/tests/utests/basic/test_xpath.c index f28df3cbd..668e1f813 100644 --- a/tests/utests/basic/test_xpath.c +++ b/tests/utests/basic/test_xpath.c @@ -1377,6 +1377,45 @@ test_no_value(void **state) lyd_free_all(tree); } +static void +test_nodeset_boolean(void **state) +{ + const char *data = "false"; + struct lyd_node *tree; + ly_bool r; + + assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); + assert_non_null(tree); + + /* an empty node-set is false */ + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo2 = false()", &r)); + assert_int_equal(r, 1); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo2 = true()", &r)); + assert_int_equal(r, 0); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo2 != false()", &r)); + assert_int_equal(r, 0); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo2 != true()", &r)); + assert_int_equal(r, 1); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "false() = /a:foo2", &r)); + assert_int_equal(r, 1); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo2 < true()", &r)); + assert_int_equal(r, 1); + + /* a non-empty node-set is true, whatever its value */ + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo = false()", &r)); + assert_int_equal(r, 0); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "/a:foo = true()", &r)); + assert_int_equal(r, 1); + + lyd_free_all(tree); +} + int main(void) { @@ -1398,6 +1437,7 @@ main(void) UTEST(test_anydata, setup), UTEST(test_number, setup), UTEST(test_no_value, setup), + UTEST(test_nodeset_boolean, setup), }; return cmocka_run_group_tests(tests, NULL, NULL);