Skip to content

Commit 9bca805

Browse files
committed
patch 8.0.1402: crash with nasty autocommand
Problem: Crash with nasty autocommand. (gy741, Dominique Pelle) Solution: Check that the new current buffer isn't wiped out. (closes #2447)
1 parent cb89c98 commit 9bca805

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

src/buffer.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,8 @@ set_curbuf(buf_T *buf, int action)
16651665
#ifdef FEAT_SYN_HL
16661666
long old_tw = curbuf->b_p_tw;
16671667
#endif
1668-
bufref_T bufref;
1668+
bufref_T newbufref;
1669+
bufref_T prevbufref;
16691670

16701671
setpcmark();
16711672
if (!cmdmod.keepalt)
@@ -1675,18 +1676,22 @@ set_curbuf(buf_T *buf, int action)
16751676
/* Don't restart Select mode after switching to another buffer. */
16761677
VIsual_reselect = FALSE;
16771678

1678-
/* close_windows() or apply_autocmds() may change curbuf */
1679+
/* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1680+
*/
16791681
prevbuf = curbuf;
1680-
set_bufref(&bufref, prevbuf);
1682+
set_bufref(&prevbufref, prevbuf);
1683+
set_bufref(&newbufref, buf);
16811684

16821685
#ifdef FEAT_AUTOCMD
1686+
/* Autocommands may delete the curren buffer and/or the buffer we wan to go
1687+
* to. In those cases don't close the buffer. */
16831688
if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
1689+
|| (bufref_valid(&prevbufref)
1690+
&& bufref_valid(&newbufref)
16841691
# ifdef FEAT_EVAL
1685-
|| (bufref_valid(&bufref) && !aborting())
1686-
# else
1687-
|| bufref_valid(&bufref)
1692+
&& !aborting()
16881693
# endif
1689-
)
1694+
))
16901695
#endif
16911696
{
16921697
#ifdef FEAT_SYN_HL
@@ -1696,9 +1701,9 @@ set_curbuf(buf_T *buf, int action)
16961701
if (unload)
16971702
close_windows(prevbuf, FALSE);
16981703
#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1699-
if (bufref_valid(&bufref) && !aborting())
1704+
if (bufref_valid(&prevbufref) && !aborting())
17001705
#else
1701-
if (bufref_valid(&bufref))
1706+
if (bufref_valid(&prevbufref))
17021707
#endif
17031708
{
17041709
win_T *previouswin = curwin;

src/testdir/test_autocmd.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,3 +1163,11 @@ func Test_TextYankPost()
11631163
unlet g:event
11641164
bwipe!
11651165
endfunc
1166+
1167+
func Test_nocatch_wipe_all_buffers()
1168+
" Real nasty autocommand: wipe all buffers on any event.
1169+
au * * bwipe *
1170+
call assert_fails('next x', 'E93')
1171+
bwipe
1172+
au!
1173+
endfunc

src/version.c

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

772772
static int included_patches[] =
773773
{ /* Add new patch number below this line */
774+
/**/
775+
1402,
774776
/**/
775777
1401,
776778
/**/

0 commit comments

Comments
 (0)