Skip to content

Commit

Permalink
Merge pull request #4760 from justinmk/term-use-after-free
Browse files Browse the repository at this point in the history
term_close use-after-free
  • Loading branch information
justinmk committed May 16, 2016
2 parents 3cc2a28 + f583bc9 commit 082abb7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
15 changes: 13 additions & 2 deletions src/nvim/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -21701,15 +21701,26 @@ static void term_resize(uint16_t width, uint16_t height, void *d)
pty_process_resize(&data->proc.pty, width, height);
}

static inline void term_delayed_free(void **argv)
{
TerminalJobData *j = argv[0];
if (j->in.pending_reqs || j->out.pending_reqs || j->err.pending_reqs) {
queue_put(j->events, term_delayed_free, 1, j);
return;
}

terminal_destroy(j->term);
term_job_data_decref(j);
}

static void term_close(void *d)
{
TerminalJobData *data = d;
if (!data->exited) {
data->exited = true;
process_stop((Process *)&data->proc);
}
terminal_destroy(data->term);
term_job_data_decref(d);
queue_put(data->events, term_delayed_free, 1, data);
}

static void term_job_data_decref(TerminalJobData *data)
Expand Down
4 changes: 1 addition & 3 deletions test/functional/legacy/068_text_formatting_spec.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
-- Test for text formatting.

local helpers = require('test.functional.helpers')
local feed, insert, source = helpers.feed, helpers.insert, helpers.source
local feed, insert = helpers.feed, helpers.insert
local clear, execute, expect = helpers.clear, helpers.execute, helpers.expect

describe('text formatting', function()
Expand Down
13 changes: 11 additions & 2 deletions test/functional/terminal/buffer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ describe('terminal buffer', function()
end)

it('handles loss of focus gracefully', function()
-- Temporarily change the statusline to avoid printing the file name, which
-- varies be where the test is run.
-- Change the statusline to avoid printing the file name, which varies.
nvim('set_option', 'statusline', '==========')
execute('set laststatus=0')

Expand Down Expand Up @@ -195,5 +194,15 @@ describe('terminal buffer', function()

execute('set laststatus=1') -- Restore laststatus to the default.
end)

it('term_close() use-after-free #4393', function()
if eval("executable('yes')") == 0 then
pending('missing "yes" command')
return
end
execute('terminal yes')
feed([[<C-\><C-n>]])
execute('bdelete!')
end)
end)

6 changes: 3 additions & 3 deletions test/functional/terminal/ex_terminal_spec.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local helpers = require('test.functional.helpers')
local Screen = require('test.functional.ui.screen')
local clear, wait, nvim = helpers.clear, helpers.wait, helpers.nvim
local nvim_dir, source, ok = helpers.nvim_dir, helpers.source, helpers.ok
local nvim_dir, source, eq = helpers.nvim_dir, helpers.source, helpers.eq
local execute, eval = helpers.execute, helpers.eval

describe(':terminal', function()
Expand Down Expand Up @@ -53,7 +53,7 @@ describe(':terminal', function()
source([[
autocmd BufNew * set shell=foo
terminal]])
-- Verify that BufNew actually fired (else the test is useless).
ok('foo' == eval('&shell'))
-- Verify that BufNew actually fired (else the test is invalid).
eq('foo', eval('&shell'))
end)
end)

0 comments on commit 082abb7

Please sign in to comment.