]> granicus.if.org Git - vim/commitdiff
patch 8.2.0120: virtcol() does not check arguments to be valid v8.2.0120
authorBram Moolenaar <Bram@vim.org>
Wed, 15 Jan 2020 19:36:55 +0000 (20:36 +0100)
committerBram Moolenaar <Bram@vim.org>
Wed, 15 Jan 2020 19:36:55 +0000 (20:36 +0100)
Problem:    virtcol() does not check arguments to be valid, which may lead to
            a crash.
Solution:   Check the column to be valid.  Do not decrement MAXCOL.
            (closes #5480)

src/evalfunc.c
src/testdir/test_marks.vim
src/version.c

index 4465fc07c11a14c46bd490dc0a6bf8c52f52e180..d248f4bf429bdd4e80cc6a42cdec279c5d34f66b 100644 (file)
@@ -6605,7 +6605,7 @@ f_setpos(typval_T *argvars, typval_T *rettv)
     {
        if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
        {
-           if (--pos.col < 0)
+           if (pos.col != MAXCOL && --pos.col < 0)
                pos.col = 0;
            if (name[0] == '.' && name[1] == NUL)
            {
@@ -8372,11 +8372,21 @@ f_virtcol(typval_T *argvars, typval_T *rettv)
     colnr_T    vcol = 0;
     pos_T      *fp;
     int                fnum = curbuf->b_fnum;
+    int                len;
 
     fp = var2fpos(&argvars[0], FALSE, &fnum);
     if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
                                                    && fnum == curbuf->b_fnum)
     {
+       // Limit the column to a valid value, getvvcol() doesn't check.
+       if (fp->col < 0)
+           fp->col = 0;
+       else
+       {
+           len = (int)STRLEN(ml_get(fp->lnum));
+           if (fp->col > len)
+               fp->col = len;
+       }
        getvvcol(curwin, fp, NULL, NULL, &vcol);
        ++vcol;
     }
index 3d04c70aa6d32a8e35d861ef0badcf08bc4fc3c7..829f40dcbd049c7a6d7f08a6db44053510bd1cf8 100644 (file)
@@ -26,11 +26,11 @@ func Test_Incr_Marks()
 endfunc
 
 func Test_setpos()
-  new one
+  new Xone
   let onebuf = bufnr('%')
   let onewin = win_getid()
   call setline(1, ['aaa', 'bbb', 'ccc'])
-  new two
+  new Xtwo
   let twobuf = bufnr('%')
   let twowin = win_getid()
   call setline(1, ['aaa', 'bbb', 'ccc'])
@@ -63,7 +63,24 @@ func Test_setpos()
   call setpos("'N", [onebuf, 1, 3, 0])
   call assert_equal([onebuf, 1, 3, 0], getpos("'N"))
 
+  " try invalid column and check virtcol()
   call win_gotoid(onewin)
+  call setpos("'a", [0, 1, 2, 0])
+  call assert_equal([0, 1, 2, 0], getpos("'a"))
+  call setpos("'a", [0, 1, -5, 0])
+  call assert_equal([0, 1, 2, 0], getpos("'a"))
+  call setpos("'a", [0, 1, 0, 0])
+  call assert_equal([0, 1, 1, 0], getpos("'a"))
+  call setpos("'a", [0, 1, 4, 0])
+  call assert_equal([0, 1, 4, 0], getpos("'a"))
+  call assert_equal(4, virtcol("'a"))
+  call setpos("'a", [0, 1, 5, 0])
+  call assert_equal([0, 1, 5, 0], getpos("'a"))
+  call assert_equal(4, virtcol("'a"))
+  call setpos("'a", [0, 1, 21341234, 0])
+  call assert_equal([0, 1, 21341234, 0], getpos("'a"))
+  call assert_equal(4, virtcol("'a"))
+
   bwipe!
   call win_gotoid(twowin)
   bwipe!
index 44bfd6bc324efa7e1d077f7fb7dcd2aee7ebc939..e20bf5d243c4a44b36dc4281039251b5516d45a4 100644 (file)
@@ -742,6 +742,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    120,
 /**/
     119,
 /**/