diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index ab560c041a..c84c7a30ce 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -2494,11 +2494,14 @@ int tv_dict_add_list(dict_T *const d, const char *const key, const size_t key_le item->di_tv.v_type = VAR_LIST; item->di_tv.vval.v_list = list; - tv_list_ref(list); if (tv_dict_add(d, item) == FAIL) { + // Detach "list" so tv_dict_item_free() does not unref it: on failure + // ownership stays with the caller. + item->di_tv.vval.v_list = NULL; tv_dict_item_free(item); return FAIL; } + tv_list_ref(list); return OK; }