if (din->din_fname == NULL)
return diff_write_buffer(buf, din, start, end);
+ // Writing the diff buffers may trigger changes in the window structure
+ // via aucmd_prepbuf()/aucmd_restbuf() commands.
+ // This may cause recursively calling winframe_remove() which is not safe and causes
+ // use after free, so let's stop it here.
+ if (frames_locked())
+ return FAIL;
+
if (end < 0)
end = buf->b_ml.ml_line_count;
/* window.c */
void window_layout_lock(void);
void window_layout_unlock(void);
+int frames_locked(void);
int window_layout_locked(enum CMD_index cmd);
int check_can_set_curbuf_disabled(void);
int check_can_set_curbuf_forceit(int forceit);
call StopVimInTerminal(buf)
endfunc
+" this was causing a use-after-free by callig winframe_remove() rerursively
+func Test_diffexpr_wipe_buffers()
+ CheckRunVimInTerminal
+
+ let lines =<< trim END
+ def DiffFuncExpr()
+ var in: list<string> = readfile(v:fname_in)
+ var new = readfile(v:fname_new)
+ var out: string = diff(in, new)
+ writefile(split(out, "n"), v:fname_out)
+ enddef
+
+ new
+ vnew
+ set diffexpr=DiffFuncExpr()
+ wincmd l
+ new
+ cal setline(1,range(20))
+ wind difft
+ wincm w
+ hid
+ %bw!
+ END
+ call writefile(lines, 'Xtest_diffexpr_wipe', 'D')
+
+ let buf = RunVimInTerminal('Xtest_diffexpr_wipe', {})
+ call term_sendkeys(buf, ":so\<CR>")
+ call WaitForAssert({-> assert_match('4 buffers wiped out', term_getline(buf, 20))})
+
+ call StopVimInTerminal(buf)
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 2085,
/**/
2084,
/**/
// autocommands mess up the window structure.
static int close_disallowed = 0;
+// When non-zero changing the window frame structure is forbidden. Used
+// to avoid that winframe_remove() is called recursively
+static int frame_locked = 0;
+
/*
* Disallow changing the window layout (split window, close window, move
* window). Resizing is still allowed.
--close_disallowed;
}
+ int
+frames_locked(void)
+{
+ return frame_locked;
+}
+
/*
* When the window layout cannot be changed give an error and return TRUE.
* "cmd" indicates the action being performed and is used to pick the relevant
if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
return NULL;
+ frame_locked++;
+
// Save the position of the containing frame (which will also contain the
// altframe) before we remove anything, to recompute window positions later.
wp = frame2win(frp_close->fr_parent);
else
*unflat_altfr = frp2;
+ frame_locked--;
+
return wp;
}