]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0406: Divide by zero with getmousepos() and 'smoothscroll' v9.1.0406
authorzeertzjq <zeertzjq@outlook.com>
Sat, 11 May 2024 09:23:37 +0000 (11:23 +0200)
committerChristian Brabandt <cb@256bit.org>
Sat, 11 May 2024 09:23:37 +0000 (11:23 +0200)
Problem:  Divide by zero with getmousepos() and 'smoothscroll'.
Solution: Don't compute skip_lines when width1 is zero.
          (zeertzjq)

closes: #14747

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/mouse.c
src/move.c
src/testdir/test_functions.vim
src/version.c

index 247c6df8e26c2b03751ce8a23e873c3e67fc99d2..4e10e723ed2a5784766a46e0075ffe40b2d52771 100644 (file)
@@ -3029,16 +3029,22 @@ mouse_comp_pos(
 
        if (win->w_skipcol > 0 && lnum == win->w_topline)
        {
-           // Adjust for 'smoothscroll' clipping the top screen lines.
-           // A similar formula is used in curs_columns().
            int width1 = win->w_width - win_col_off(win);
-           int skip_lines = 0;
-           if (win->w_skipcol > width1)
-               skip_lines = (win->w_skipcol - width1)
+
+           if (width1 > 0)
+           {
+               int skip_lines = 0;
+
+               // Adjust for 'smoothscroll' clipping the top screen lines.
+               // A similar formula is used in curs_columns().
+               if (win->w_skipcol > width1)
+                   skip_lines = (win->w_skipcol - width1)
                                            / (width1 + win_col_off2(win)) + 1;
-           else if (win->w_skipcol > 0)
-               skip_lines = 1;
-           count -= skip_lines;
+               else if (win->w_skipcol > 0)
+                   skip_lines = 1;
+
+               count -= skip_lines;
+           }
        }
 
        if (count > row)
index 6790192ab5c041a0b0c31cc0cafa9c9d0bc78d6c..797184812ad75431fc51fae9bf176fc5cd469210 100644 (file)
@@ -2617,12 +2617,14 @@ scroll_cursor_bot(int min_scroll, int set_topbot)
                            plines_win
 #endif
                                        (curwin, curwin->w_topline, FALSE);
-           int skip_lines = 0;
            int width1 = curwin->w_width - curwin_col_off();
+
            if (width1 > 0)
            {
                int width2 = width1 + curwin_col_off2();
-               // similar formula is used in curs_columns()
+               int skip_lines = 0;
+
+               // A similar formula is used in curs_columns().
                if (curwin->w_skipcol > width1)
                    skip_lines += (curwin->w_skipcol - width1) / width2 + 1;
                else if (curwin->w_skipcol > 0)
index f0d738582506dc8af2bc726b783bb537e8e30948..5adbb42e65bf8306acab335f1f60e4e19061c49b 100644 (file)
@@ -3697,6 +3697,73 @@ func Test_getmousepos()
         \ column: 8,
         \ coladd: 21,
         \ }, getmousepos())
+
+  30vnew
+  setlocal smoothscroll number
+  call setline(1, join(range(100)))
+  exe "normal! \<C-E>"
+  call test_setmouse(1, 5)
+  call assert_equal(#{
+        \ screenrow: 1,
+        \ screencol: 5,
+        \ winid: win_getid(),
+        \ winrow: 1,
+        \ wincol: 5,
+        \ line: 1,
+        \ column: 27,
+        \ coladd: 0,
+        \ }, getmousepos())
+  call test_setmouse(2, 5)
+  call assert_equal(#{
+        \ screenrow: 2,
+        \ screencol: 5,
+        \ winid: win_getid(),
+        \ winrow: 2,
+        \ wincol: 5,
+        \ line: 1,
+        \ column: 53,
+        \ coladd: 0,
+        \ }, getmousepos())
+
+  exe "normal! \<C-E>"
+  call test_setmouse(1, 5)
+  call assert_equal(#{
+        \ screenrow: 1,
+        \ screencol: 5,
+        \ winid: win_getid(),
+        \ winrow: 1,
+        \ wincol: 5,
+        \ line: 1,
+        \ column: 53,
+        \ coladd: 0,
+        \ }, getmousepos())
+  call test_setmouse(2, 5)
+  call assert_equal(#{
+        \ screenrow: 2,
+        \ screencol: 5,
+        \ winid: win_getid(),
+        \ winrow: 2,
+        \ wincol: 5,
+        \ line: 1,
+        \ column: 79,
+        \ coladd: 0,
+        \ }, getmousepos())
+
+  vert resize 4
+  call test_setmouse(2, 2)
+  " This used to crash Vim
+  call assert_equal(#{
+        \ screenrow: 2,
+        \ screencol: 2,
+        \ winid: win_getid(),
+        \ winrow: 2,
+        \ wincol: 2,
+        \ line: 1,
+        \ column: 53,
+        \ coladd: 0,
+        \ }, getmousepos())
+
+  bwipe!
   bwipe!
 endfunc
 
index cbd29097d8aba5560415145bbf258fbf0d0f674d..e601c821727bd759c38afafca129ba610449c4cd 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    406,
 /**/
     405,
 /**/