From abdb802483042ca6c913cc2b5cddfff79082b08f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Sun, 28 Apr 2024 23:17:48 +0200 Subject: [PATCH] Fix value< string comparison ungoodthink * src/fns.c (string_cmp): Fix bad comparisons for certain strings. This only affected `value<` for aggregates, not `string<`. * test/src/fns-tests.el (fns-value<-ordered): Add test cases. (cherry picked from commit 9b1e44c7fb5281688488ec077c048e268b716ad2) --- src/fns.c | 8 ++++---- test/src/fns-tests.el | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/fns.c b/src/fns.c index 5cdd7e67f31..d4ba0601889 100644 --- a/src/fns.c +++ b/src/fns.c @@ -481,7 +481,7 @@ string_cmp (Lisp_Object string1, Lisp_Object string2) int d = memcmp (SSDATA (string1), SSDATA (string2), n); if (d) return d; - return n < SCHARS (string2) ? -1 : n > SCHARS (string2); + return n < SCHARS (string2) ? -1 : n < SCHARS (string1); } else if (STRING_MULTIBYTE (string1) && STRING_MULTIBYTE (string2)) { @@ -515,7 +515,7 @@ string_cmp (Lisp_Object string1, Lisp_Object string2) if (b >= nb) /* One string is a prefix of the other. */ - return b < nb2 ? -1 : b > nb2; + return b < nb2 ? -1 : b < nb1; /* Now back up to the start of the differing characters: it's the last byte not having the bit pattern 10xxxxxx. */ @@ -540,7 +540,7 @@ string_cmp (Lisp_Object string1, Lisp_Object string2) if (c1 != c2) return c1 < c2 ? -1 : 1; } - return i1 < SCHARS (string2) ? -1 : i1 > SCHARS (string2); + return i1 < SCHARS (string2) ? -1 : i1 < SCHARS (string1); } else { @@ -553,7 +553,7 @@ string_cmp (Lisp_Object string1, Lisp_Object string2) if (c1 != c2) return c1 < c2 ? -1 : 1; } - return i1 < SCHARS (string2) ? -1 : i1 > SCHARS (string2); + return i1 < SCHARS (string2) ? -1 : i1 < SCHARS (string1); } } diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el index 068daf893f1..a73a7a42f26 100644 --- a/test/src/fns-tests.el +++ b/test/src/fns-tests.el @@ -1614,6 +1614,12 @@ ;; strings ("" . "a") ("a" . "b") ("A" . "a") ("abc" . "abd") ("b" . "ba") + ;; strings again, but in a context where 3-way comparison + ;; matters + (("" . 2) . ("a" . 1)) + (("å" . 2) . ("åü" . 1)) + (("a" . 2) . ("aå" . 1)) + (("\x80" . 2) . ("\x80å" . 1)) ;; lists ((1 2 3) . (2 3 4)) ((2) . (2 1)) (() . (0)) -- 2.39.5