]> granicus.if.org Git - vim/commitdiff
patch 8.2.3910: when compare function of sort() fails it does not abort v8.2.3910
authorBram Moolenaar <Bram@vim.org>
Mon, 27 Dec 2021 11:54:37 +0000 (11:54 +0000)
committerBram Moolenaar <Bram@vim.org>
Mon, 27 Dec 2021 11:54:37 +0000 (11:54 +0000)
Problem:    When the compare function of sort() produces and error then sort()
            does not abort.
Solution:   Check if did_emsg was incremented.

src/list.c
src/testdir/test_vim9_builtin.vim
src/version.c

index 6fafef6371faf37182d6ebf79f01d81c9de26a58..838991b733faec83b78c8acf1692c83042d73074 100644 (file)
@@ -1919,6 +1919,7 @@ item_compare2(const void *s1, const void *s2)
     char_u     *func_name;
     partial_T  *partial = sortinfo->item_compare_partial;
     funcexe_T  funcexe;
+    int                did_emsg_before = did_emsg;
 
     // shortcut after failure in previous call; compare all items equal
     if (sortinfo->item_compare_func_err)
@@ -1946,7 +1947,7 @@ item_compare2(const void *s1, const void *s2)
     clear_tv(&argv[0]);
     clear_tv(&argv[1]);
 
-    if (res == FAIL)
+    if (res == FAIL || did_emsg > did_emsg_before)
        res = ITEM_COMPARE_FAIL;
     else
     {
index 088afe842e60ddd31e248f4699d2beb56097e05c..6cad1e4cc4e43a052984195fd3545e8601d2a3df 100644 (file)
@@ -3524,6 +3524,17 @@ def Test_sort_argument()
   CheckDefAndScriptFailure(['sort([1], "", [1])'], ['E1013: Argument 3: type mismatch, expected dict<any> but got list<number>', 'E1206: Dictionary required for argument 3'])
 enddef
 
+def Test_sort_compare_func_fails()
+  var lines =<< trim END
+    vim9script
+    echo ['a', 'b', 'c']->sort((a: number, b: number) => 0)
+  END
+  writefile(lines, 'Xbadsort')
+  assert_fails('source Xbadsort', ['E1013:', 'E702:'])
+
+  delete('Xbadsort')
+enddef
+
 def Test_spellbadword()
   CheckDefAndScriptFailure(['spellbadword(100)'], ['E1013: Argument 1: type mismatch, expected string but got number', 'E1174: String required for argument 1'])
   spellbadword('good')->assert_equal(['', ''])
index 62b646c33f5619847d2a6675b7d52218d899b164..8c4377b1e7237fa2852ab2339cff527f63798c7c 100644 (file)
@@ -749,6 +749,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3910,
 /**/
     3909,
 /**/