0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

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 #5095)
This commit is contained in:
Bram Moolenaar 2019-10-21 23:08:59 +02:00
parent 34ba06b6e6
commit 396b7c78c0
3 changed files with 31 additions and 4 deletions

View File

@ -251,18 +251,23 @@ linelen(int *has_tab)
int save; int save;
int len; 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(); line = ml_get_curline();
if (*line == NUL)
return 0;
// find the first non-blank character
first = skipwhite(line); 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); for (last = first + STRLEN(first);
last > first && VIM_ISWHITE(last[-1]); --last) last > first && VIM_ISWHITE(last[-1]); --last)
; ;
save = *last; save = *last;
*last = NUL; *last = NUL;
len = linetabsize(line); /* get line length */ len = linetabsize(line); // get line length
if (has_tab != NULL) /* check for embedded TAB */ if (has_tab != NULL) // check for embedded TAB
*has_tab = (vim_strchr(first, TAB) != NULL); *has_tab = (vim_strchr(first, TAB) != NULL);
*last = save; *last = save;

View File

@ -1,4 +1,7 @@
" Tests for the various 'formatoptions' settings " Tests for the various 'formatoptions' settings
source check.vim
func Test_text_format() func Test_text_format()
enew! enew!
@ -489,3 +492,20 @@ func Test_format_list_auto()
bwipe! bwipe!
set fo& ai& bs& set fo& ai& bs&
endfunc 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

View File

@ -741,6 +741,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
2198,
/**/ /**/
2197, 2197,
/**/ /**/