Skip to content

Commit d46ae14

Browse files
committed
patch 7.4.1331
Problem: Crash when closing the channel in a callback. (Christian J. Robinson) Solution: Take the callback out of the list before invoking it.
1 parent 0943a09 commit d46ae14

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/channel.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -888,8 +888,7 @@ channel_parse_json(channel_T *channel)
888888
}
889889

890890
/*
891-
* Remove "node" from the queue that it is in and free it.
892-
* Also frees the contained callback name.
891+
* Remove "node" from the queue that it is in. Does not free it.
893892
*/
894893
static void
895894
remove_cb_node(cbq_T *head, cbq_T *node)
@@ -902,8 +901,6 @@ remove_cb_node(cbq_T *head, cbq_T *node)
902901
head->cq_prev = node->cq_prev;
903902
else
904903
node->cq_next->cq_prev = node->cq_prev;
905-
vim_free(node->cq_callback);
906-
vim_free(node);
907904
}
908905

909906
/*
@@ -1144,8 +1141,12 @@ may_invoke_callback(channel_T *channel)
11441141
if (item->cq_seq_nr == seq_nr)
11451142
{
11461143
ch_log(channel, "Invoking one-time callback\n");
1147-
invoke_callback(channel, item->cq_callback, argv);
1144+
/* Remove the item from the list first, if the callback
1145+
* invokes ch_close() the list will be cleared. */
11481146
remove_cb_node(head, item);
1147+
invoke_callback(channel, item->cq_callback, argv);
1148+
vim_free(item->cq_callback);
1149+
vim_free(item);
11491150
done = TRUE;
11501151
break;
11511152
}
@@ -1329,7 +1330,13 @@ channel_clear(channel_T *channel)
13291330
vim_free(channel_get(channel));
13301331

13311332
while (cb_head->cq_next != NULL)
1332-
remove_cb_node(cb_head, cb_head->cq_next);
1333+
{
1334+
cbq_T *node = cb_head->cq_next;
1335+
1336+
remove_cb_node(cb_head, node);
1337+
vim_free(node->cq_callback);
1338+
vim_free(node);
1339+
}
13331340

13341341
while (json_head->jq_next != NULL)
13351342
{

src/testdir/test_channel.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ func Test_pipe()
301301
endtry
302302
endfunc
303303

304+
""""""""""
305+
304306
let s:unletResponse = ''
305307
func s:UnletHandler(handle, msg)
306308
let s:unletResponse = a:msg
@@ -319,6 +321,28 @@ func Test_unlet_handle()
319321
call s:run_server('s:unlet_handle')
320322
endfunc
321323

324+
""""""""""
325+
326+
let s:unletResponse = ''
327+
func s:CloseHandler(handle, msg)
328+
let s:unletResponse = a:msg
329+
call ch_close(s:channelfd)
330+
endfunc
331+
332+
" Test that "unlet handle" in a handler doesn't crash Vim.
333+
func s:close_handle(port)
334+
let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
335+
call ch_sendexpr(s:channelfd, "test", function('s:CloseHandler'))
336+
sleep 10m
337+
call assert_equal('what?', s:unletResponse)
338+
endfunc
339+
340+
func Test_close_handle()
341+
call s:run_server('s:close_handle')
342+
endfunc
343+
344+
""""""""""
345+
322346
func Test_open_fail()
323347
silent! let ch = ch_open("noserver")
324348
echo ch

src/version.c

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

748748
static int included_patches[] =
749749
{ /* Add new patch number below this line */
750+
/**/
751+
1331,
750752
/**/
751753
1330,
752754
/**/

0 commit comments

Comments
 (0)