Skip to content

Commit cc7f8be

Browse files
committed
patch 7.4.1465
Problem: Coverity reported possible use of NULL pointer when using buffer output with JSON mode. Solution: Make it actually possible to use JSON mode with a buffer. Re-encode the JSON to append it to the buffer.
1 parent 5131c14 commit cc7f8be

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

src/channel.c

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,9 @@ channel_set_options(channel_T *channel, jobopt_T *opt)
926926

927927
if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
928928
{
929-
/* writing output to a buffer. Force mode to NL. */
930-
channel->ch_part[PART_OUT].ch_mode = MODE_NL;
929+
/* writing output to a buffer. Default mode is NL. */
930+
if (!(opt->jo_set & JO_OUT_MODE))
931+
channel->ch_part[PART_OUT].ch_mode = MODE_NL;
931932
channel->ch_part[PART_OUT].ch_buffer =
932933
find_buffer(opt->jo_io_name[PART_OUT]);
933934
ch_logs(channel, "writing to buffer '%s'",
@@ -1560,32 +1561,38 @@ may_invoke_callback(channel_T *channel, int part)
15601561
u_sync(TRUE);
15611562
u_save(lnum, lnum + 1);
15621563

1563-
ml_append(lnum, msg, 0, FALSE);
1564-
appended_lines_mark(lnum, 1L);
1565-
curbuf = save_curbuf;
1566-
1567-
if (buffer->b_nwindows > 0)
1564+
if (msg == NULL)
1565+
/* JSON or JS mode: re-encode the message. */
1566+
msg = json_encode(listtv, ch_mode);
1567+
if (msg != NULL)
15681568
{
1569-
win_T *wp;
1570-
win_T *save_curwin;
1569+
ml_append(lnum, msg, 0, FALSE);
1570+
appended_lines_mark(lnum, 1L);
1571+
curbuf = save_curbuf;
15711572

1572-
FOR_ALL_WINDOWS(wp)
1573+
if (buffer->b_nwindows > 0)
15731574
{
1574-
if (wp->w_buffer == buffer
1575-
&& wp->w_cursor.lnum == lnum
1576-
&& wp->w_cursor.col == 0)
1575+
win_T *wp;
1576+
win_T *save_curwin;
1577+
1578+
FOR_ALL_WINDOWS(wp)
15771579
{
1578-
++wp->w_cursor.lnum;
1579-
save_curwin = curwin;
1580-
curwin = wp;
1581-
curbuf = curwin->w_buffer;
1582-
scroll_cursor_bot(0, FALSE);
1583-
curwin = save_curwin;
1584-
curbuf = curwin->w_buffer;
1580+
if (wp->w_buffer == buffer
1581+
&& wp->w_cursor.lnum == lnum
1582+
&& wp->w_cursor.col == 0)
1583+
{
1584+
++wp->w_cursor.lnum;
1585+
save_curwin = curwin;
1586+
curwin = wp;
1587+
curbuf = curwin->w_buffer;
1588+
scroll_cursor_bot(0, FALSE);
1589+
curwin = save_curwin;
1590+
curbuf = curwin->w_buffer;
1591+
}
15851592
}
1593+
redraw_buf_later(buffer, VALID);
1594+
channel_need_redraw = TRUE;
15861595
}
1587-
redraw_buf_later(buffer, VALID);
1588-
channel_need_redraw = TRUE;
15891596
}
15901597
}
15911598
if (callback != NULL)

src/testdir/test_channel.vim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,32 @@ func Test_pipe_to_nameless_buffer()
426426
endtry
427427
endfunc
428428

429+
func Test_pipe_to_buffer_json()
430+
if !has('job')
431+
return
432+
endif
433+
call ch_log('Test_pipe_to_buffer_json()')
434+
let job = job_start(s:python . " test_channel_pipe.py",
435+
\ {'out-io': 'buffer', 'out-mode': 'json'})
436+
call assert_equal("run", job_status(job))
437+
try
438+
let handle = job_getchannel(job)
439+
call ch_sendraw(handle, "echo [0, \"hello\"]\n")
440+
call ch_sendraw(handle, "echo [-2, 12.34]\n")
441+
exe ch_getbufnr(handle, "out") . 'sbuf'
442+
for i in range(100)
443+
sleep 10m
444+
if line('$') >= 3
445+
break
446+
endif
447+
endfor
448+
call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
449+
bwipe!
450+
finally
451+
call job_stop(job)
452+
endtry
453+
endfunc
454+
429455
""""""""""
430456

431457
let s:unletResponse = ''

src/version.c

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

744744
static int included_patches[] =
745745
{ /* Add new patch number below this line */
746+
/**/
747+
1465,
746748
/**/
747749
1464,
748750
/**/

0 commit comments

Comments
 (0)