Skip to content

Commit 63acae1

Browse files
yegappanbrammool
authored andcommitted
patch 9.0.0204: indexof() may leak memory
Problem: indexof() may leak memory. Solution: Free allocated values. (Yegappan Lakshmanan, closes #10916)
1 parent c9b6570 commit 63acae1

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

src/evalfunc.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6814,6 +6814,7 @@ indexof_eval_expr(typval_T *expr)
68146814
return FALSE;
68156815

68166816
found = tv_get_bool_chk(&newtv, &error);
6817+
clear_tv(&newtv);
68176818

68186819
return error ? FALSE : found;
68196820
}
@@ -6864,6 +6865,7 @@ indexof_list(list_T *l, long startidx, typval_T *expr)
68646865
{
68656866
listitem_T *item;
68666867
long idx = 0;
6868+
int found;
68676869

68686870
if (l == NULL)
68696871
return -1;
@@ -6888,7 +6890,10 @@ indexof_list(list_T *l, long startidx, typval_T *expr)
68886890
set_vim_var_nr(VV_KEY, idx);
68896891
copy_tv(&item->li_tv, get_vim_var_tv(VV_VAL));
68906892

6891-
if (indexof_eval_expr(expr))
6893+
found = indexof_eval_expr(expr);
6894+
clear_tv(get_vim_var_tv(VV_VAL));
6895+
6896+
if (found)
68926897
return idx;
68936898
}
68946899

src/testdir/test_blob.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ func Test_indexof()
772772
call assert_equal(-1, indexof(b, {i, v -> v == 0x1}))
773773
call assert_equal(1, indexof(b, "v:val == 0xad"))
774774
call assert_equal(-1, indexof(b, "v:val == 0xff"))
775+
call assert_equal(-1, indexof(b, {_, v -> "v == 0xad"}))
775776

776777
call assert_equal(-1, indexof(0z, "v:val == 0x0"))
777778
call assert_equal(-1, indexof(test_null_blob(), "v:val == 0xde"))

src/testdir/test_listdict.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,13 @@ func Test_indexof()
14621462
call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: -4}))
14631463
call assert_equal(0, indexof(l, "v:val.n == 10", test_null_dict()))
14641464

1465+
let s = ["a", "b", "c"]
1466+
call assert_equal(2, indexof(s, {_, v -> v == 'c'}))
1467+
call assert_equal(-1, indexof(s, {_, v -> v == 'd'}))
1468+
call assert_equal(-1, indexof(s, {_, v -> "v == 'd'"}))
1469+
14651470
call assert_equal(-1, indexof([], {i, v -> v == 'a'}))
1471+
call assert_equal(-1, indexof([1, 2, 3], {_, v -> "v == 2"}))
14661472
call assert_equal(-1, indexof(test_null_list(), {i, v -> v == 'a'}))
14671473
call assert_equal(-1, indexof(l, test_null_string()))
14681474
call assert_equal(-1, indexof(l, test_null_function()))

src/testdir/test_vim9_builtin.vim

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,10 +2074,36 @@ def Test_indexof()
20742074
var b = 0zdeadbeef
20752075
indexof(b, "v:val == 0xef")->assert_equal(3)
20762076

2077-
def TestIdx(k: number, v: dict<any>): bool
2077+
def TestIdx1(k: number, v: dict<any>): bool
20782078
return v.color == 'blue'
20792079
enddef
2080-
indexof(l, TestIdx)->assert_equal(1)
2080+
indexof(l, TestIdx1)->assert_equal(1)
2081+
2082+
var lines =<< trim END
2083+
def TestIdx(v: dict<any>): bool
2084+
return v.color == 'blue'
2085+
enddef
2086+
2087+
indexof([{color: "red"}], TestIdx)
2088+
END
2089+
v9.CheckDefAndScriptFailure(lines, ['E176: Invalid number of arguments', 'E118: Too many arguments for function'])
2090+
2091+
lines =<< trim END
2092+
def TestIdx(k: number, v: dict<any>)
2093+
enddef
2094+
2095+
indexof([{color: "red"}], TestIdx)
2096+
END
2097+
v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?number, ?any): bool', 'E1031: Cannot use void value'])
2098+
2099+
lines =<< trim END
2100+
def TestIdx(k: number, v: dict<any>): string
2101+
return "abc"
2102+
enddef
2103+
2104+
indexof([{color: "red"}], TestIdx)
2105+
END
2106+
v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?number, ?any): bool', 'E1135: Using a String as a Bool'])
20812107
enddef
20822108

20832109
def Test_input()

src/version.c

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

736736
static int included_patches[] =
737737
{ /* Add new patch number below this line */
738+
/**/
739+
204,
738740
/**/
739741
203,
740742
/**/

0 commit comments

Comments
 (0)