]> granicus.if.org Git - vim/commitdiff
patch 8.2.2820: session file may divide by zero v8.2.2820
authorBram Moolenaar <Bram@vim.org>
Fri, 30 Apr 2021 19:37:51 +0000 (21:37 +0200)
committerBram Moolenaar <Bram@vim.org>
Fri, 30 Apr 2021 19:37:51 +0000 (21:37 +0200)
Problem:    Session file may divide by zero.
Solution:   Avoid writing difide by zero. (closes #8162)

src/session.c
src/testdir/test_mksession.vim
src/version.c

index 6d1ccb57e2019a4f6403e5d2f3770550cbe98a9c..8edf0dade79aab374ff7d74efab8edf53a0a9e55 100644 (file)
@@ -456,11 +456,19 @@ put_view(
 
        // Restore the cursor line in the file and relatively in the
        // window.  Don't use "G", it changes the jumplist.
-       if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
+       if (wp->w_height <= 0)
+       {
+           if (fprintf(fd, "let s:l = %ld", (long)wp->w_cursor.lnum) < 0)
+               return FAIL;
+       }
+       else if (fprintf(fd,
+                   "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
                    (long)wp->w_cursor.lnum,
                    (long)(wp->w_cursor.lnum - wp->w_topline),
-                   (long)wp->w_height / 2, (long)wp->w_height) < 0
-               || put_eol(fd) == FAIL
+                   (long)wp->w_height / 2, (long)wp->w_height) < 0)
+           return FAIL;
+
+       if (put_eol(fd) == FAIL
                || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
                || put_line(fd, "keepjumps exe s:l") == FAIL
                || put_line(fd, "normal! zt") == FAIL
index 045aa2de0a5c03e07e5a2277fb100e486e998f34..e876f987c1760a7b38dbf13ff670c5f134c70e82 100644 (file)
@@ -179,6 +179,20 @@ func Test_mksession_large_winheight()
   call delete('Xtest_mks_winheight.out')
 endfunc
 
+func Test_mksession_zero_winheight()
+  set winminheight=0
+  edit SomeFile
+  split
+  wincmd _
+  mksession! Xtest_mks_zero
+  set winminheight&
+  let text = readfile('Xtest_mks_zero')->join()
+  "call delete('Xtest_mks_zero')
+  close
+  " check there is no devide by zero
+  call assert_notmatch('/ 0[^0-9]', text)
+endfunc
+
 func Test_mksession_rtp()
   " TODO: fix problem with backslashes on Win32
   CheckNotMSWindows
index b1ba0a11975065c5c4cbce6a28d82afd355a72bf..ed632f7ba949126a71d52e0c693099d4b7014584 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2820,
 /**/
     2819,
 /**/