From 186ab2bbc0ac26bae65a680975b952ce6c1bbd09 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Sat, 7 Apr 2012 21:48:48 +0200
Subject: [PATCH] array: lfun_cmp() mustn't use LFUN_EQ if LFUN_GT and LFUN_LT
 are lacking.

---
 src/array.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/array.c b/src/array.c
index 15ad6505d0..f55f392ed2 100644
--- a/src/array.c
+++ b/src/array.c
@@ -932,7 +932,13 @@ static int lfun_cmp (const struct svalue *a, const struct svalue *b)
       default_res = CMPFUN_UNORDERED;
     }
 
-    if ((fun = FIND_LFUN(p->inherits[SUBTYPEOF(*a)].prog, LFUN_EQ)) != -1) {
+    /* NB: It's not a good idea to use LFUN_EQ here if
+     *     there is neither LFUN_LT nor LFUN_GT, since
+     *     the sorting order may get confused, which
+     *     will cause merge_array_with_order() to fail.
+     */
+    if ((default_res == CMPFUN_UNORDERED) &&
+	(fun = FIND_LFUN(p->inherits[SUBTYPEOF(*a)].prog, LFUN_EQ)) != -1) {
       push_svalue(b);
       apply_low(a->u.object,
 		fun + p->inherits[SUBTYPEOF(*a)].identifier_level, 1);
@@ -971,7 +977,13 @@ static int lfun_cmp (const struct svalue *a, const struct svalue *b)
       default_res = CMPFUN_UNORDERED;
     }
 
-    if ((fun = FIND_LFUN(p->inherits[SUBTYPEOF(*b)].prog, LFUN_EQ)) != -1) {
+    /* NB: It's not a good idea to use LFUN_EQ here if
+     *     there is neither LFUN_LT nor LFUN_GT, since
+     *     the sorting order may get confused, which
+     *     will cause merge_array_with_order() to fail.
+     */
+    if ((default_res == CMPFUN_UNORDERED) &&
+	(fun = FIND_LFUN(p->inherits[SUBTYPEOF(*b)].prog, LFUN_EQ)) != -1) {
       push_svalue(a);
       apply_low(b->u.object,
 		fun + p->inherits[SUBTYPEOF(*b)].identifier_level, 1);
-- 
GitLab