]> granicus.if.org Git - vim/commitdiff
patch 8.1.2198: crash when using :center in autocommand v8.1.2198
authorBram Moolenaar <Bram@vim.org>
Mon, 21 Oct 2019 21:08:59 +0000 (23:08 +0200)
committerBram Moolenaar <Bram@vim.org>
Mon, 21 Oct 2019 21:08:59 +0000 (23:08 +0200)
Problem:    Crash when using :center in autocommand.
Solution:   Bail out early for an empty line. (Dominique pelle, closes #5095)

src/ex_cmds.c
src/testdir/test_textformat.vim
src/version.c

index bcff7ee71678e2c293a7bca96d5e0206a135e749..e472c8a6fc84d54a1decdd3f8d526341f402451b 100644 (file)
@@ -251,18 +251,23 @@ linelen(int *has_tab)
     int            save;
     int            len;
 
-    /* find the first non-blank character */
+    // Get the line.  If it's empty bail out early (could be the empty string
+    // for an unloaded buffer).
     line = ml_get_curline();
+    if (*line == NUL)
+       return 0;
+
+    // find the first non-blank character
     first = skipwhite(line);
 
-    /* find the character after the last non-blank character */
+    // find the character after the last non-blank character
     for (last = first + STRLEN(first);
                                last > first && VIM_ISWHITE(last[-1]); --last)
        ;
     save = *last;
     *last = NUL;
-    len = linetabsize(line);           /* get line length */
-    if (has_tab != NULL)               /* check for embedded TAB */
+    len = linetabsize(line);           // get line length
+    if (has_tab != NULL)               // check for embedded TAB
        *has_tab = (vim_strchr(first, TAB) != NULL);
     *last = save;
 
index 13fb50b9854ad38ece827482fb9fef16d9bf20ad..3a0552b834fd4b7e30a8c8ff9f942f131cc1d4fa 100644 (file)
@@ -1,4 +1,7 @@
 " Tests for the various 'formatoptions' settings
+
+source check.vim
+
 func Test_text_format()
   enew!
 
@@ -489,3 +492,20 @@ func Test_format_list_auto()
   bwipe!
   set fo& ai& bs&
 endfunc
+
+func Test_crash_github_issue_5095()
+  CheckFeature autocmd
+
+  " This used to segfault, see https://github.com/vim/vim/issues/5095
+  augroup testing
+    au BufNew x center
+  augroup END
+
+  next! x
+
+  bw
+  augroup testing
+    au!
+  augroup END
+  augroup! testing
+endfunc
index 549b7eea7e651f06c73925e8b1737a3906929a78..77e6e35bec51ab28cfd9c45bb1deb9ead79cab0e 100644 (file)
@@ -741,6 +741,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2198,
 /**/
     2197,
 /**/