summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-01-26 11:55:16 +0100
committerJo-Philipp Wich <jo@mein.io>2022-01-26 11:58:08 +0100
commitddc5aa7cd3121300f2ba6e68cb038258a616d4e4 (patch)
tree63585f32e5334f6e7ea9f4693e94d740744e5482
parent58e1da9b7dd69af0f32fb2a70c1808dce8c51733 (diff)
vm: fix NaN strict equality tests
A performance shortcut in `ucv_is_equal()` incorrectly led to `NaN === NaN` being true. Fix the issue by only comparing pointers when the involved types are not doubles. Due to fixing `NaN !== NaN`, the `uniq()` function now requires a special case to treat multiple NaNs equal for the sake of generating an array of unique values. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--lib.c5
-rw-r--r--tests/custom/04_bugs/30_nan_strict_equality14
-rw-r--r--types.c2
3 files changed, 20 insertions, 1 deletions
diff --git a/lib.c b/lib.c
index 9d7b26a..7b0c9ca 100644
--- a/lib.c
+++ b/lib.c
@@ -2921,6 +2921,11 @@ uc_uniq_ucv_equal(const void *k1, const void *k2)
if (!ucv_is_scalar(uv1) && !ucv_is_scalar(uv2))
return (uv1 == uv2);
+ /* for the sake of array item uniqueness, treat two NaNs as equal */
+ if (ucv_type(uv1) == UC_DOUBLE && ucv_type(uv2) == UC_DOUBLE &&
+ isnan(ucv_double_get(uv1)) && isnan(ucv_double_get(uv2)))
+ return true;
+
return ucv_is_equal(uv1, uv2);
}
diff --git a/tests/custom/04_bugs/30_nan_strict_equality b/tests/custom/04_bugs/30_nan_strict_equality
new file mode 100644
index 0000000..4ec32e2
--- /dev/null
+++ b/tests/custom/04_bugs/30_nan_strict_equality
@@ -0,0 +1,14 @@
+When comparing `nan` with `nan` for strict equality or inequality, the
+VM incorrectly treated the result as `true` or `false` respectively.
+
+-- Testcase --
+{{ NaN === NaN }}
+{{ NaN !== NaN }}
+{{ uniq([NaN, NaN]) }}
+-- End --
+
+-- Expect stdout --
+false
+true
+[ "NaN" ]
+-- End --
diff --git a/types.c b/types.c
index 5ea1180..dac0efb 100644
--- a/types.c
+++ b/types.c
@@ -1764,7 +1764,7 @@ ucv_is_equal(uc_value_t *uv1, uc_value_t *uv2)
if (t1 != t2)
return false;
- if (uv1 == uv2)
+ if (t1 != UC_DOUBLE && uv1 == uv2)
return true;
switch (t1) {