Merge pull request #20160 from zeertzjq/vim-8.2.2646

vim-patch:8.2.{2646,2664}: string argument type check
This commit is contained in:
zeertzjq 2022-09-12 15:29:39 +08:00 committed by GitHub
commit 738c204523
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 17 deletions

View File

@ -1772,7 +1772,7 @@ static void f_eventhandler(typval_T *argvars, typval_T *rettv, EvalFuncData fptr
/// "executable()" function
static void f_executable(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
if (tv_check_for_string(&argvars[0]) == FAIL) {
if (tv_check_for_string_arg(argvars, 0) == FAIL) {
return;
}
@ -1901,7 +1901,7 @@ static void f_win_execute(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
/// "exepath()" function
static void f_exepath(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
if (tv_check_for_nonempty_string(&argvars[0]) == FAIL) {
if (tv_check_for_nonempty_string_arg(argvars, 0) == FAIL) {
return;
}

View File

@ -40,6 +40,11 @@
# include "eval/typval.c.generated.h"
#endif
static char e_string_required_for_argument_nr[]
= N_("E1174: String required for argument %d");
static char e_non_empty_string_required_for_argument_nr[]
= N_("E1142: Non-empty string required for argument %d");
bool tv_in_free_unref_items = false;
// TODO(ZyX-I): Remove DICT_MAXNEST, make users be non-recursive instead
@ -3800,26 +3805,26 @@ float_T tv_get_float(const typval_T *const tv)
return 0;
}
// Give an error and return FAIL unless "tv" is a string.
int tv_check_for_string(const typval_T *const tv)
/// Give an error and return FAIL unless "args[idx]" is a string.
int tv_check_for_string_arg(const typval_T *const args, const int idx)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
{
if (tv->v_type != VAR_STRING) {
emsg(_(e_stringreq));
if (args[idx].v_type != VAR_STRING) {
semsg(_(e_string_required_for_argument_nr), idx + 1);
return FAIL;
}
return OK;
}
// Give an error and return FAIL unless "tv" is a non-empty string.
int tv_check_for_nonempty_string(const typval_T *const tv)
/// Give an error and return FAIL unless "args[idx]" is a non-empty string.
int tv_check_for_nonempty_string_arg(const typval_T *const args, const int idx)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
{
if (tv_check_for_string(tv) == FAIL) {
if (tv_check_for_string_arg(args, idx) == FAIL) {
return FAIL;
}
if (tv->vval.v_string == NULL || *tv->vval.v_string == NUL) {
emsg(_(e_non_empty_string_required));
if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL) {
semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
return FAIL;
}
return OK;

View File

@ -2793,7 +2793,7 @@ void f_setcellwidths(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
void f_charclass(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
if (tv_check_for_string(&argvars[0]) == FAIL
if (tv_check_for_string_arg(argvars, 0) == FAIL
|| argvars[0].vval.v_string == NULL) {
return;
}

View File

@ -34,11 +34,13 @@ describe('executable()', function()
it('fails for invalid values', function()
for _, input in ipairs({'v:null', 'v:true', 'v:false', '{}', '[]'}) do
eq('Vim(call):E928: String required', exc_exec('call executable('..input..')'))
eq('Vim(call):E1174: String required for argument 1',
exc_exec('call executable('..input..')'))
end
command('let $PATH = fnamemodify("./test/functional/fixtures/bin", ":p")')
for _, input in ipairs({'v:null', 'v:true', 'v:false'}) do
eq('Vim(call):E928: String required', exc_exec('call executable('..input..')'))
eq('Vim(call):E1174: String required for argument 1',
exc_exec('call executable('..input..')'))
end
end)

View File

@ -21,12 +21,12 @@ describe('exepath()', function()
it('fails for invalid values', function()
for _, input in ipairs({'v:null', 'v:true', 'v:false', '{}', '[]'}) do
eq('Vim(call):E928: String required', exc_exec('call exepath('..input..')'))
eq('Vim(call):E1174: String required for argument 1', exc_exec('call exepath('..input..')'))
end
eq('Vim(call):E1142: Non-empty string required', exc_exec('call exepath("")'))
eq('Vim(call):E1142: Non-empty string required for argument 1', exc_exec('call exepath("")'))
command('let $PATH = fnamemodify("./test/functional/fixtures/bin", ":p")')
for _, input in ipairs({'v:null', 'v:true', 'v:false'}) do
eq('Vim(call):E928: String required', exc_exec('call exepath('..input..')'))
eq('Vim(call):E1174: String required for argument 1', exc_exec('call exepath('..input..')'))
end
end)