• R/O
  • SSH

vim: Commit

Mirror of the Vim source from https://github.com/vim/vim


Commit MetaInfo

Revisãod406858354a6a05d7e6ca164e7dcf8c8acc1cfc0 (tree)
Hora2021-04-09 03:15:03
AutorBram Moolenaar <Bram@vim....>
CommiterBram Moolenaar

Mensagem de Log

patch 8.2.2738: extending a list with itself can give wrong result

Commit: https://github.com/vim/vim/commit/dcae51facc4d6de1edd62f0242b40972be841103
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Apr 8 20:10:10 2021 +0200

patch 8.2.2738: extending a list with itself can give wrong result
Problem: Extending a list with itself can give wrong result.
Solution: Remember the item before where the insertion happens and skip to
after the already inserted items. (closes #1112)

Mudança Sumário

Diff

diff -r 5cd55703c76d -r d406858354a6 src/list.c
--- a/src/list.c Thu Apr 08 18:30:04 2021 +0200
+++ b/src/list.c Thu Apr 08 20:15:03 2021 +0200
@@ -894,6 +894,7 @@
894894 {
895895 listitem_T *item;
896896 int todo;
897+ listitem_T *bef_prev;
897898
898899 // NULL list is equivalent to an empty list: nothing to do.
899900 if (l2 == NULL || l2->lv_len == 0)
@@ -903,9 +904,15 @@
903904 CHECK_LIST_MATERIALIZE(l1);
904905 CHECK_LIST_MATERIALIZE(l2);
905906
907+ // When exending a list with itself, at some point we run into the item
908+ // that was before "bef" and need to skip over the already inserted items
909+ // to "bef".
910+ bef_prev = bef == NULL ? NULL : bef->li_prev;
911+
906912 // We also quit the loop when we have inserted the original item count of
907913 // the list, avoid a hang when we extend a list with itself.
908- for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
914+ for (item = l2->lv_first; item != NULL && --todo >= 0;
915+ item = item == bef_prev ? bef : item->li_next)
909916 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
910917 return FAIL;
911918 return OK;
diff -r 5cd55703c76d -r d406858354a6 src/testdir/test_listdict.vim
--- a/src/testdir/test_listdict.vim Thu Apr 08 18:30:04 2021 +0200
+++ b/src/testdir/test_listdict.vim Thu Apr 08 20:15:03 2021 +0200
@@ -862,6 +862,20 @@
862862
863863 " Extend g: dictionary with an invalid variable name
864864 call assert_fails("call extend(g:, {'-!' : 10})", 'E461:')
865+
866+ " Extend a list with itself.
867+ let l = [1, 5, 7]
868+ call extend(l, l, 0)
869+ call assert_equal([1, 5, 7, 1, 5, 7], l)
870+ let l = [1, 5, 7]
871+ call extend(l, l, 1)
872+ call assert_equal([1, 1, 5, 7, 5, 7], l)
873+ let l = [1, 5, 7]
874+ call extend(l, l, 2)
875+ call assert_equal([1, 5, 1, 5, 7, 7], l)
876+ let l = [1, 5, 7]
877+ call extend(l, l, 3)
878+ call assert_equal([1, 5, 7, 1, 5, 7], l)
865879 endfunc
866880
867881 func Test_listdict_extendnew()
diff -r 5cd55703c76d -r d406858354a6 src/version.c
--- a/src/version.c Thu Apr 08 18:30:04 2021 +0200
+++ b/src/version.c Thu Apr 08 20:15:03 2021 +0200
@@ -751,6 +751,8 @@
751751 static int included_patches[] =
752752 { /* Add new patch number below this line */
753753 /**/
754+ 2738,
755+/**/
754756 2737,
755757 /**/
756758 2736,
Show on old repository browser