commit 5370b7a2e0a0484c9005cb5a727dffa5ef13b1ed
parent d6be2b33125d6706779beed7f8e83015bdff9396
Author: zeertzjq <zeertzjq@outlook.com>
Date: Sat, 6 Dec 2025 21:49:16 +0800
vim-patch:partial:9.1.1955: sort() does not handle large numbers correctly (#36840)
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: vim/vim#18868
https://github.com/vim/vim/commit/04794efe12863eb96a489531c299879e6c8d15d4
Use a Lua test for now, as the Vimscript test uses tuples.
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Diffstat:
2 files changed, 27 insertions(+), 6 deletions(-)
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
@@ -1285,12 +1285,8 @@ static int item_compare2(const void *s1, const void *s2, bool keep_zero)
res = ITEM_COMPARE_FAIL;
sortinfo->item_compare_func_err = true;
} 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 = 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
diff --git a/test/functional/vimscript/sort_spec.lua b/test/functional/vimscript/sort_spec.lua
@@ -66,4 +66,29 @@ describe('sort()', function()
pcall_err(command, 'let sl = sort([1, 0, [], 3, 2], "Cmp")')
)
end)
+
+ it('handles large numbers properly', function()
+ local to_sort = {
+ { 229539777187355, 229539777187355 },
+ { 487766135067138, 491977135306566 },
+ { 188325333471071, 188931909913550 },
+ { 264028451845520, 265514296554744 },
+ { 245727634348687, 249469249579525 },
+ { 375117820166731, 378942174241518 },
+ { 535474757750378, 535849288071548 },
+ }
+ local expected = {
+ { 188325333471071, 188931909913550 },
+ { 229539777187355, 229539777187355 },
+ { 245727634348687, 249469249579525 },
+ { 264028451845520, 265514296554744 },
+ { 375117820166731, 378942174241518 },
+ { 487766135067138, 491977135306566 },
+ { 535474757750378, 535849288071548 },
+ }
+ eq(expected, fn.sort(to_sort))
+ api.nvim_set_var('to_sort', to_sort)
+ eq(expected, api.nvim_eval('sort(g:to_sort)'))
+ eq(expected, api.nvim_eval('sort(g:to_sort, {a, b -> a[0] - b[0]})'))
+ end)
end)