0
0
mirror of https://github.com/vim/vim.git synced 2025-09-26 04:04:07 -04:00

patch 8.2.2365: Vim9: no check for map() changing item type at script level

Problem:    Vim9: no check for map() changing item type at script level.
Solution:   Check the new value type.
This commit is contained in:
Bram Moolenaar
2021-01-16 19:01:53 +01:00
parent f898f7c68d
commit 70250fb4d2
4 changed files with 80 additions and 29 deletions

View File

@@ -1985,10 +1985,18 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
: N_("filter() argument"));
int save_did_emsg;
int idx = 0;
type_T *type = NULL;
garray_T type_list;
// map() and filter() return the first argument, also on failure.
if (filtermap != FILTERMAP_MAPNEW)
copy_tv(&argvars[0], rettv);
if (filtermap == FILTERMAP_MAP && in_vim9script())
{
// Check that map() does not change the type of the dict.
ga_init2(&type_list, sizeof(type_T *), 10);
type = typval2type(argvars, &type_list);
}
if (argvars[0].v_type == VAR_BLOB)
{
@@ -1998,7 +2006,7 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
rettv->vval.v_blob = NULL;
}
if ((b = argvars[0].vval.v_blob) == NULL)
return;
goto theend;
}
else if (argvars[0].v_type == VAR_LIST)
{
@@ -2010,7 +2018,7 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
if ((l = argvars[0].vval.v_list) == NULL
|| (filtermap == FILTERMAP_FILTER
&& value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
return;
goto theend;
}
else if (argvars[0].v_type == VAR_DICT)
{
@@ -2022,12 +2030,12 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
if ((d = argvars[0].vval.v_dict) == NULL
|| (filtermap == FILTERMAP_FILTER
&& value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
return;
goto theend;
}
else
{
semsg(_(e_listdictblobarg), ermsg);
return;
goto theend;
}
expr = &argvars[1];
@@ -2055,7 +2063,7 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
if (filtermap == FILTERMAP_MAPNEW)
{
if (rettv_dict_alloc(rettv) == FAIL)
return;
goto theend;
d_ret = rettv->vval.v_dict;
}
@@ -2090,6 +2098,12 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
}
if (filtermap == FILTERMAP_MAP)
{
if (type != NULL && check_typval_type(type->tt_member,
&newtv, 0) == FAIL)
{
clear_tv(&newtv);
break;
}
// map(): replace the dict item value
clear_tv(&di->di_tv);
newtv.v_lock = 0;
@@ -2126,7 +2140,7 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
if (filtermap == FILTERMAP_MAPNEW)
{
if (blob_copy(b, rettv) == FAIL)
return;
goto theend;
b_ret = rettv->vval.v_blob;
}
@@ -2175,7 +2189,7 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
if (filtermap == FILTERMAP_MAPNEW)
{
if (rettv_list_alloc(rettv) == FAIL)
return;
goto theend;
l_ret = rettv->vval.v_list;
}
// set_vim_var_nr() doesn't set the type
@@ -2218,6 +2232,13 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
}
if (filtermap != FILTERMAP_FILTER)
{
if (filtermap == FILTERMAP_MAP && type != NULL
&& check_typval_type(type->tt_member,
&newtv, 0) == FAIL)
{
clear_tv(&newtv);
break;
}
// map(), mapnew(): always append the new value to the
// list
if (list_append_tv_move(filtermap == FILTERMAP_MAP
@@ -2256,6 +2277,12 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
}
if (filtermap == FILTERMAP_MAP)
{
if (type != NULL && check_typval_type(type->tt_member,
&newtv, 0) == FAIL)
{
clear_tv(&newtv);
break;
}
// map(): replace the list item value
clear_tv(&li->li_tv);
newtv.v_lock = 0;
@@ -2281,6 +2308,10 @@ filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
did_emsg |= save_did_emsg;
}
theend:
if (type != NULL)
clear_type_list(&type_list);
}
/*

View File

@@ -1351,7 +1351,7 @@ def Test_var_list_dict_type()
var ll: list<number>
ll = [1, 2, 3]->map('"one"')
END
CheckDefExecFailure(lines, 'E1012: Type mismatch; expected list<number> but got list<string>')
CheckDefExecFailure(lines, 'E1012: Type mismatch; expected number but got string')
enddef
def Test_cannot_use_let()

View File

@@ -331,27 +331,6 @@ def Wrong_dict_key_type(items: list<number>): list<number>
return filter(items, (_, val) => get({[val]: 1}, 'x'))
enddef
def Test_map_function_arg()
var lines =<< trim END
def MapOne(i: number, v: string): string
return i .. ':' .. v
enddef
var l = ['a', 'b', 'c']
map(l, MapOne)
assert_equal(['0:a', '1:b', '2:c'], l)
END
CheckDefAndScriptSuccess(lines)
enddef
def Test_map_item_type()
var lines =<< trim END
var l = ['a', 'b', 'c']
map(l, (k, v) => k .. '/' .. v )
assert_equal(['0/a', '1/b', '2/c'], l)
END
CheckDefAndScriptSuccess(lines)
enddef
def Test_filereadable()
assert_false(filereadable(""))
assert_false(filereadable(test_null_string()))
@@ -584,6 +563,45 @@ def SID(): number
->str2nr()
enddef
def Test_map_function_arg()
var lines =<< trim END
def MapOne(i: number, v: string): string
return i .. ':' .. v
enddef
var l = ['a', 'b', 'c']
map(l, MapOne)
assert_equal(['0:a', '1:b', '2:c'], l)
END
CheckDefAndScriptSuccess(lines)
enddef
def Test_map_item_type()
var lines =<< trim END
var l = ['a', 'b', 'c']
map(l, (k, v) => k .. '/' .. v )
assert_equal(['0/a', '1/b', '2/c'], l)
END
CheckDefAndScriptSuccess(lines)
lines =<< trim END
var l: list<number> = [0]
echo map(l, (_, v) => [])
END
CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
lines =<< trim END
var l: list<number> = range(2)
echo map(l, (_, v) => [])
END
CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
lines =<< trim END
var d: dict<number> = {key: 0}
echo map(d, (_, v) => [])
END
CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got list<unknown>', 2)
enddef
def Test_maparg()
var lnum = str2nr(expand('<sflnum>'))
map foo bar

View File

@@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
2365,
/**/
2364,
/**/