Skip to content

Commit 2a77d21

Browse files
committed
patch 8.0.1647: terminal API may call any user function
Problem: Terminal API may call a function not meant to be called by this API. Solution: Require the function to start with Tapi_.
1 parent 4368d5c commit 2a77d21

4 files changed

Lines changed: 45 additions & 17 deletions

File tree

‎runtime/doc/terminal.txt‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -423,20 +423,26 @@ Currently supported commands:
423423

424424
call {funcname} {argument}
425425

426-
Call a user defined function with [argument]. The function is
427-
called with the buffer number of the terminal and the decoded
428-
argument. The user function must sanity check the argument.
426+
Call a user defined function with {argument}.
427+
The function is called with two arguments: the buffer number
428+
of the terminal and {argument}, the decoded JSON argument.
429+
The function name must start with "Tapi_" to avoid
430+
accidentally calling a function not meant to be used for the
431+
terminal API
432+
The user function should sanity check the argument.
429433
The function can use |term_sendkeys()| to send back a reply.
430434
Example in JSON: >
431-
["call", "Impression", ["play", 14]]
435+
["call", "Tapi_Impression", ["play", 14]]
432436
< Calls a function defined like this: >
433-
function Impression(bufnum, arglist)
437+
function Tapi_Impression(bufnum, arglist)
434438
if len(a:arglist) == 2
435-
echo "impression " . a:arglist[0]
436-
echo "count " . a:arglist[1]
439+
echomsg "impression " . a:arglist[0]
440+
echomsg "count " . a:arglist[1]
437441
endif
438442
endfunc
439-
<
443+
< Output from `:echo` may be erased by a redraw, use `:echomsg`
444+
to be able to see it with `:messages`.
445+
440446
drop {filename}
441447

442448
Let Vim open a file, like the `:drop` command. If {filename}
@@ -447,7 +453,7 @@ Currently supported commands:
447453
448454
A trick to have Vim send this escape sequence: >
449455
exe "set t_ts=\<Esc>]51; t_fs=\x07"
450-
let &titlestring = '["call","TryThis",["hello",123]]'
456+
let &titlestring = '["call","Tapi_TryThis",["hello",123]]'
451457
redraw
452458
set t_ts& t_fs&
453459

‎src/terminal.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3193,7 +3193,7 @@ handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
31933193
}
31943194
func = get_tv_string(&item->li_tv);
31953195

3196-
if (!ASCII_ISUPPER(*func))
3196+
if (STRNCMP(func, "Tapi_", 5) != 0)
31973197
{
31983198
ch_log(channel, "Invalid function name: %s", func);
31993199
return;

‎src/testdir/test_terminal.vim‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,24 +1072,28 @@ func Test_terminal_api_drop_oldwin()
10721072
bwipe Xtextfile
10731073
endfunc
10741074

1075-
func TryThis(bufnum, arg)
1075+
func Tapi_TryThis(bufnum, arg)
10761076
let g:called_bufnum = a:bufnum
10771077
let g:called_arg = a:arg
10781078
endfunc
10791079

1080-
func Test_terminal_api_call()
1081-
if !CanRunVimInTerminal()
1082-
return
1083-
endif
1084-
1080+
func WriteApiCall(funcname)
10851081
" Use the title termcap entries to output the escape sequence.
10861082
call writefile([
10871083
\ 'set title',
10881084
\ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1089-
\ 'let &titlestring = ''["call","TryThis",["hello",123]]''',
1085+
\ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
10901086
\ 'redraw',
10911087
\ "set t_ts=",
10921088
\ ], 'Xscript')
1089+
endfunc
1090+
1091+
func Test_terminal_api_call()
1092+
if !CanRunVimInTerminal()
1093+
return
1094+
endif
1095+
1096+
call WriteApiCall('Tapi_TryThis')
10931097
let buf = RunVimInTerminal('-S Xscript', {})
10941098
call WaitFor({-> exists('g:called_bufnum')})
10951099
call assert_equal(buf, g:called_bufnum)
@@ -1100,3 +1104,19 @@ func Test_terminal_api_call()
11001104
unlet g:called_bufnum
11011105
unlet g:called_arg
11021106
endfunc
1107+
1108+
func Test_terminal_api_call_fails()
1109+
if !CanRunVimInTerminal()
1110+
return
1111+
endif
1112+
1113+
call WriteApiCall('TryThis')
1114+
call ch_logfile('Xlog', 'w')
1115+
let buf = RunVimInTerminal('-S Xscript', {})
1116+
call WaitFor({-> string(readfile('Xlog')) =~ 'Invalid function name: TryThis'})
1117+
1118+
call StopVimInTerminal(buf)
1119+
call delete('Xscript')
1120+
call ch_logfile('', '')
1121+
call delete('Xlog')
1122+
endfunc

‎src/version.c‎

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

767767
static int included_patches[] =
768768
{ /* Add new patch number below this line */
769+
/**/
770+
1647,
769771
/**/
770772
1646,
771773
/**/

0 commit comments

Comments
 (0)