]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.3182: Vim9: crash when using removing items from a constant list v8.2.3182
authorBram Moolenaar <Bram@vim.org>
Sun, 18 Jul 2021 20:25:29 +0000 (22:25 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 18 Jul 2021 20:25:29 +0000 (22:25 +0200)
Problem:    Vim9: crash when using removing items from a constant list.
            (Yegappan Lakshmanan)
Solution:   When a list was allocated with items copy them.

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

index d138d868cdad8fe3b594e32d2a50587df7668827..417f735e0dba4f44c1488782da82d921a55ed0a9 100644 (file)
@@ -1566,12 +1566,32 @@ list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
                    vimlist_remove(l, item, item2);
                    if (rettv_list_alloc(rettv) == OK)
                    {
-                       l = rettv->vval.v_list;
-                       l->lv_first = item;
-                       l->lv_u.mat.lv_last = item2;
-                       item->li_prev = NULL;
-                       item2->li_next = NULL;
-                       l->lv_len = cnt;
+                       list_T *rl = rettv->vval.v_list;
+
+                       if (l->lv_with_items > 0)
+                       {
+                           // need to copy the list items and move the value
+                           while (item != NULL)
+                           {
+                               li = listitem_alloc();
+                               if (li == NULL)
+                                   return;
+                               li->li_tv = item->li_tv;
+                               init_tv(&item->li_tv);
+                               list_append(rl, li);
+                               if (item == item2)
+                                   break;
+                               item = item->li_next;
+                           }
+                       }
+                       else
+                       {
+                           rl->lv_first = item;
+                           rl->lv_u.mat.lv_last = item2;
+                           item->li_prev = NULL;
+                           item2->li_next = NULL;
+                           rl->lv_len = cnt;
+                       }
                    }
                }
            }
index 638d7daa13f9f340301e484a12ba3cb2735399ca..76987b68a310ea3fd3c91fe8ffb039451b1f308f 100644 (file)
@@ -2083,6 +2083,12 @@ def Test_remote_startserver()
   CheckDefFailure(['remote_startserver({})'], 'E1013: Argument 1: type mismatch, expected string but got dict<unknown>')
 enddef
 
+def Test_remove_const_list()
+  var l: list<number> = [1, 2, 3, 4]
+  assert_equal([1, 2], remove(l, 0, 1))
+  assert_equal([3, 4], l)
+enddef
+
 def Test_remove_return_type()
   var l = remove({one: [1, 2], two: [3, 4]}, 'one')
   var res = 0
index 371fdcd88132ec556486a33fc94cb7265b287a79..6ae1d3bf07ec4ba40687eae04ae37b15bbdb8b67 100644 (file)
@@ -755,6 +755,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3182,
 /**/
     3181,
 /**/