]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1955: sort() does not handle large numbers correctly v9.1.1955
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sat, 6 Dec 2025 09:22:07 +0000 (10:22 +0100)
committerChristian Brabandt <cb@256bit.org>
Sat, 6 Dec 2025 09:22:07 +0000 (10:22 +0100)
Problem:  sort() does not handle large numbers correctly
          (Igbanam Ogbuluijah)
Solution: Don't truncate the return value of tv_get_number_chk()
          (Yegappan Lakshmanan)

closes: #18868

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/list.c
src/testdir/test_sort.vim
src/version.c

index f8c365af824a43f57fa659a20ae896c2a57bc91d..7d6793c59e2f112ee7e7798c11ca33cb7eef7970 100644 (file)
@@ -2083,11 +2083,10 @@ item_compare2(const void *s1, const void *s2)
        res = ITEM_COMPARE_FAIL;
     else
     {
-       res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
-       if (res > 0)
-           res = 1;
-       else if (res < 0)
-           res = -1;
+       varnumber_T     n;
+
+       n = tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
+       res = (n > 0) ? 1 : (n < 0) ? -1 : 0;
     }
     if (sortinfo->item_compare_func_err)
        res = ITEM_COMPARE_FAIL;  // return value has wrong type
index 60b0212428d036cefd67a5783e3805eefeb2c38c..39c4114f015bce66cb4e3b64758c3af895ed075e 100644 (file)
@@ -1,5 +1,7 @@
 " Tests for the "sort()" function and for the ":sort" command.
 
+import './util/vim9.vim' as v9
+
 func Compare1(a, b) abort
   call sort(range(3), 'Compare2')
   return a:a - a:b
@@ -1557,4 +1559,30 @@ func Test_sort_using_dict_func()
   delfunc DictSort
 endfunc
 
+" Test for using sort() function with a funcref and large numbers
+func Test_sort_funcref_with_large_number()
+  let lines =<< trim END
+    call assert_equal(
+        \ [
+        \  (188325333471071, 188931909913550),
+        \  (229539777187355, 229539777187355),
+        \  (245727634348687, 249469249579525),
+        \  (264028451845520, 265514296554744),
+        \  (375117820166731, 378942174241518),
+        \  (487766135067138, 491977135306566),
+        \  (535474757750378, 535849288071548)
+        \ ],
+        \ [
+        \  (229539777187355, 229539777187355),
+        \  (487766135067138, 491977135306566),
+        \  (188325333471071, 188931909913550),
+        \  (264028451845520, 265514296554744),
+        \  (245727634348687, 249469249579525),
+        \  (375117820166731, 378942174241518),
+        \  (535474757750378, 535849288071548)
+        \ ]->sort(LSTART a, b LMIDDLE a[0] - b[0] LEND))
+  END
+  call v9.CheckSourceLegacyAndVim9Success(lines)
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
index bb964b62a5aadd6228ec51674c90e527007bb1d0..3df7fce94359d92adc660321555d5b6abec67f31 100644 (file)
@@ -729,6 +729,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1955,
 /**/
     1954,
 /**/