From 0706c55ab1bc9a8987b18cd25ceb509801a71038 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 6 Dec 2025 21:49:16 +0800 Subject: [PATCH] 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 (cherry picked from commit 5370b7a2e0a0484c9005cb5a727dffa5ef13b1ed) --- src/nvim/eval/typval.c | 8 ++------ test/functional/vimscript/sort_spec.lua | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index f9cf245e50..f2cf23bc15 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -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 diff --git a/test/functional/vimscript/sort_spec.lua b/test/functional/vimscript/sort_spec.lua index c99a6ebadd..c52d705393 100644 --- 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)