From 5f7f7b2a87c346788581a38d2a55633be5319d86 Mon Sep 17 00:00:00 2001 From: Eric Parsonage Date: Sat, 5 Dec 2026 15:23:57 +1030 Subject: [PATCH] xpath BUGFIX comparison of NaN Numbers are compared as printed text to avoid precision issues, so NaN, printed as "nan", was equal to itself and ordered against other numbers by its text. XPath 1.0 section 3.4 compares numbers according to IEEE 754, under which every comparison with NaN is false except '!=', which is true. Fixes #2667 --- src/xpath.c | 6 ++++++ tests/utests/basic/test_xpath.c | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/xpath.c b/src/xpath.c index b44784dc6..41d6a9ae5 100644 --- a/src/xpath.c +++ b/src/xpath.c @@ -8284,6 +8284,12 @@ moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_ assert(set1->type == set2->type); + /* NaN is unordered, so every comparison with it is false except '!=' (IEEE 754) */ + if ((set1->type == LYXP_SET_NUMBER) && (isnan(set1->val.num) || isnan(set2->val.num))) { + *result = (op[0] == '!') ? 1 : 0; + return LY_SUCCESS; + } + /* compute result */ if (op[0] == '=') { if (set1->type == LYXP_SET_BOOLEAN) { diff --git a/tests/utests/basic/test_xpath.c b/tests/utests/basic/test_xpath.c index f28df3cbd..f058bf9f5 100644 --- a/tests/utests/basic/test_xpath.c +++ b/tests/utests/basic/test_xpath.c @@ -1323,6 +1323,25 @@ test_number(void **state) assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "-0 = 0", &r)); assert_int_equal(r, 1); + /* NaN */ + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "number('x') = number('x')", &r)); + assert_int_equal(r, 0); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "number('x') != number('x')", &r)); + assert_int_equal(r, 1); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "number('x') < 5", &r)); + assert_int_equal(r, 0); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "number('x') >= 5", &r)); + assert_int_equal(r, 0); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "'' >= ''", &r)); + assert_int_equal(r, 0); + + assert_int_equal(LY_SUCCESS, lyd_eval_xpath(tree, "5 != number('x')", &r)); + assert_int_equal(r, 1); + lyd_free_siblings(tree); }