Skip to content

Commit

Permalink
eval: Do not access list item after removing it
Browse files Browse the repository at this point in the history
  • Loading branch information
ZyX-I committed Feb 13, 2018
1 parent df23769 commit d08ad17
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/nvim/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -13298,9 +13298,7 @@ static void f_remove(typval_T *argvars, typval_T *rettv, FunPtr fptr)
} else {
if (argvars[2].v_type == VAR_UNKNOWN) {
// Remove one item, return its value.
tv_list_drop_items(l, item, item);
*rettv = *TV_LIST_ITEM_TV(item);
xfree(item);
*rettv = tv_list_pop_item(l, item);
} else {
// Remove range of items, return list with values.
end = tv_get_number_chk(&argvars[2], &error);
Expand Down
15 changes: 15 additions & 0 deletions src/nvim/eval/typval.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,21 @@ void tv_list_remove_items(list_T *const l, listitem_T *const item,
tv_list_drop_items(l, item, item2);
}

/// Remove single item without freeing it
///
/// @param l List to remove item from.
/// @param item Item to remove.
///
/// @return Contents of the removed item.
typval_T tv_list_pop_item(list_T *const l, listitem_T *const item)
FUNC_ATTR_NONNULL_ALL
{
list_log(l, item, NULL, "pop");
const typval_T ret_tv = *TV_LIST_ITEM_TV(item);
tv_list_drop_items(l, item, item);
return ret_tv;
}

/// Move items "item" to "item2" from list "l" to the end of the list "tgt_l"
///
/// @param[out] l List to move from.
Expand Down

0 comments on commit d08ad17

Please sign in to comment.