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

04794efe12

Use a Lua test for now, as the Vimscript test uses tuples.

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
(cherry picked from commit 5370b7a2e0)
This commit is contained in:
zeertzjq
2025-12-06 21:49:16 +08:00
committed by github-actions[bot]
parent 5ca2eb5e48
commit 0706c55ab1
2 changed files with 27 additions and 6 deletions

View File

@@ -1287,12 +1287,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

View File

@@ -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)