Skip to content

Commit 396b7c7

Browse files
committed
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)
1 parent 34ba06b commit 396b7c7

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

src/ex_cmds.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,23 @@ linelen(int *has_tab)
251251
int save;
252252
int len;
253253

254-
/* find the first non-blank character */
254+
// Get the line. If it's empty bail out early (could be the empty string
255+
// for an unloaded buffer).
255256
line = ml_get_curline();
257+
if (*line == NUL)
258+
return 0;
259+
260+
// find the first non-blank character
256261
first = skipwhite(line);
257262

258-
/* find the character after the last non-blank character */
263+
// find the character after the last non-blank character
259264
for (last = first + STRLEN(first);
260265
last > first && VIM_ISWHITE(last[-1]); --last)
261266
;
262267
save = *last;
263268
*last = NUL;
264-
len = linetabsize(line); /* get line length */
265-
if (has_tab != NULL) /* check for embedded TAB */
269+
len = linetabsize(line); // get line length
270+
if (has_tab != NULL) // check for embedded TAB
266271
*has_tab = (vim_strchr(first, TAB) != NULL);
267272
*last = save;
268273

src/testdir/test_textformat.vim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
" Tests for the various 'formatoptions' settings
2+
3+
source check.vim
4+
25
func Test_text_format()
36
enew!
47

@@ -489,3 +492,20 @@ func Test_format_list_auto()
489492
bwipe!
490493
set fo& ai& bs&
491494
endfunc
495+
496+
func Test_crash_github_issue_5095()
497+
CheckFeature autocmd
498+
499+
" This used to segfault, see https://github.com/vim/vim/issues/5095
500+
augroup testing
501+
au BufNew x center
502+
augroup END
503+
504+
next! x
505+
506+
bw
507+
augroup testing
508+
au!
509+
augroup END
510+
augroup! testing
511+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ static char *(features[]) =
741741

742742
static int included_patches[] =
743743
{ /* Add new patch number below this line */
744+
/**/
745+
2198,
744746
/**/
745747
2197,
746748
/**/

0 commit comments

Comments
 (0)