forked from aniani/vim
patch 9.0.0204: indexof() may leak memory
Problem: indexof() may leak memory. Solution: Free allocated values. (Yegappan Lakshmanan, closes #10916)
This commit is contained in:
committed by
Bram Moolenaar
parent
c9b6570fab
commit
63acae13f5
@@ -6814,6 +6814,7 @@ indexof_eval_expr(typval_T *expr)
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
found = tv_get_bool_chk(&newtv, &error);
|
found = tv_get_bool_chk(&newtv, &error);
|
||||||
|
clear_tv(&newtv);
|
||||||
|
|
||||||
return error ? FALSE : found;
|
return error ? FALSE : found;
|
||||||
}
|
}
|
||||||
@@ -6864,6 +6865,7 @@ indexof_list(list_T *l, long startidx, typval_T *expr)
|
|||||||
{
|
{
|
||||||
listitem_T *item;
|
listitem_T *item;
|
||||||
long idx = 0;
|
long idx = 0;
|
||||||
|
int found;
|
||||||
|
|
||||||
if (l == NULL)
|
if (l == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
@@ -6888,7 +6890,10 @@ indexof_list(list_T *l, long startidx, typval_T *expr)
|
|||||||
set_vim_var_nr(VV_KEY, idx);
|
set_vim_var_nr(VV_KEY, idx);
|
||||||
copy_tv(&item->li_tv, get_vim_var_tv(VV_VAL));
|
copy_tv(&item->li_tv, get_vim_var_tv(VV_VAL));
|
||||||
|
|
||||||
if (indexof_eval_expr(expr))
|
found = indexof_eval_expr(expr);
|
||||||
|
clear_tv(get_vim_var_tv(VV_VAL));
|
||||||
|
|
||||||
|
if (found)
|
||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -772,6 +772,7 @@ func Test_indexof()
|
|||||||
call assert_equal(-1, indexof(b, {i, v -> v == 0x1}))
|
call assert_equal(-1, indexof(b, {i, v -> v == 0x1}))
|
||||||
call assert_equal(1, indexof(b, "v:val == 0xad"))
|
call assert_equal(1, indexof(b, "v:val == 0xad"))
|
||||||
call assert_equal(-1, indexof(b, "v:val == 0xff"))
|
call assert_equal(-1, indexof(b, "v:val == 0xff"))
|
||||||
|
call assert_equal(-1, indexof(b, {_, v -> "v == 0xad"}))
|
||||||
|
|
||||||
call assert_equal(-1, indexof(0z, "v:val == 0x0"))
|
call assert_equal(-1, indexof(0z, "v:val == 0x0"))
|
||||||
call assert_equal(-1, indexof(test_null_blob(), "v:val == 0xde"))
|
call assert_equal(-1, indexof(test_null_blob(), "v:val == 0xde"))
|
||||||
|
|||||||
@@ -1462,7 +1462,13 @@ func Test_indexof()
|
|||||||
call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: -4}))
|
call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: -4}))
|
||||||
call assert_equal(0, indexof(l, "v:val.n == 10", test_null_dict()))
|
call assert_equal(0, indexof(l, "v:val.n == 10", test_null_dict()))
|
||||||
|
|
||||||
|
let s = ["a", "b", "c"]
|
||||||
|
call assert_equal(2, indexof(s, {_, v -> v == 'c'}))
|
||||||
|
call assert_equal(-1, indexof(s, {_, v -> v == 'd'}))
|
||||||
|
call assert_equal(-1, indexof(s, {_, v -> "v == 'd'"}))
|
||||||
|
|
||||||
call assert_equal(-1, indexof([], {i, v -> v == 'a'}))
|
call assert_equal(-1, indexof([], {i, v -> v == 'a'}))
|
||||||
|
call assert_equal(-1, indexof([1, 2, 3], {_, v -> "v == 2"}))
|
||||||
call assert_equal(-1, indexof(test_null_list(), {i, v -> v == 'a'}))
|
call assert_equal(-1, indexof(test_null_list(), {i, v -> v == 'a'}))
|
||||||
call assert_equal(-1, indexof(l, test_null_string()))
|
call assert_equal(-1, indexof(l, test_null_string()))
|
||||||
call assert_equal(-1, indexof(l, test_null_function()))
|
call assert_equal(-1, indexof(l, test_null_function()))
|
||||||
|
|||||||
@@ -2074,10 +2074,36 @@ def Test_indexof()
|
|||||||
var b = 0zdeadbeef
|
var b = 0zdeadbeef
|
||||||
indexof(b, "v:val == 0xef")->assert_equal(3)
|
indexof(b, "v:val == 0xef")->assert_equal(3)
|
||||||
|
|
||||||
def TestIdx(k: number, v: dict<any>): bool
|
def TestIdx1(k: number, v: dict<any>): bool
|
||||||
return v.color == 'blue'
|
return v.color == 'blue'
|
||||||
enddef
|
enddef
|
||||||
indexof(l, TestIdx)->assert_equal(1)
|
indexof(l, TestIdx1)->assert_equal(1)
|
||||||
|
|
||||||
|
var lines =<< trim END
|
||||||
|
def TestIdx(v: dict<any>): bool
|
||||||
|
return v.color == 'blue'
|
||||||
|
enddef
|
||||||
|
|
||||||
|
indexof([{color: "red"}], TestIdx)
|
||||||
|
END
|
||||||
|
v9.CheckDefAndScriptFailure(lines, ['E176: Invalid number of arguments', 'E118: Too many arguments for function'])
|
||||||
|
|
||||||
|
lines =<< trim END
|
||||||
|
def TestIdx(k: number, v: dict<any>)
|
||||||
|
enddef
|
||||||
|
|
||||||
|
indexof([{color: "red"}], TestIdx)
|
||||||
|
END
|
||||||
|
v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?number, ?any): bool', 'E1031: Cannot use void value'])
|
||||||
|
|
||||||
|
lines =<< trim END
|
||||||
|
def TestIdx(k: number, v: dict<any>): string
|
||||||
|
return "abc"
|
||||||
|
enddef
|
||||||
|
|
||||||
|
indexof([{color: "red"}], TestIdx)
|
||||||
|
END
|
||||||
|
v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?number, ?any): bool', 'E1135: Using a String as a Bool'])
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
def Test_input()
|
def Test_input()
|
||||||
|
|||||||
@@ -735,6 +735,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
204,
|
||||||
/**/
|
/**/
|
||||||
203,
|
203,
|
||||||
/**/
|
/**/
|
||||||
|
|||||||
Reference in New Issue
Block a user