diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 096f11a35b..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; } @@ -2537,11 +2540,14 @@ int tv_dict_add_dict(dict_T *const d, const char *const key, const size_t key_le item->di_tv.v_type = VAR_DICT; item->di_tv.vval.v_dict = dict; - dict->dv_refcount++; if (tv_dict_add(d, item) == FAIL) { + // Detach "dict" so tv_dict_item_free() does not unref it: on failure + // ownership stays with the caller. + item->di_tv.vval.v_dict = NULL; tv_dict_item_free(item); return FAIL; } + dict->dv_refcount++; return OK; }