# HG changeset patch # User Bram Moolenaar # Date 1571692503 -7200 # Node ID 50c0a9fb07aee6b5b7d896b3fdb6977ae5be980a # Parent 3f9986aeb41ae95622376850ac9a908ab2214991 patch 8.1.2198: crash when using :center in autocommand Commit: https://github.com/vim/vim/commit/396b7c78c0fd9cd07528963b18c27398491df40d Author: Bram Moolenaar Date: Mon Oct 21 23:08:59 2019 +0200 patch 8.1.2198: crash when using :center in autocommand Problem: Crash when using :center in autocommand. Solution: Bail out early for an empty line. (Dominique pelle, closes https://github.com/vim/vim/issues/5095) diff --git a/src/ex_cmds.c b/src/ex_cmds.c --- a/src/ex_cmds.c +++ b/src/ex_cmds.c @@ -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; diff --git a/src/testdir/test_textformat.vim b/src/testdir/test_textformat.vim --- a/src/testdir/test_textformat.vim +++ b/src/testdir/test_textformat.vim @@ -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 diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -742,6 +742,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 2198, +/**/ 2197, /**/ 2196,