To: vim_dev@googlegroups.com Subject: Patch 9.0.1416 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.1416 Problem: Crash when collection is modified when using filter(). Solution: Lock the list/dict/blob. (Ernie Rael, closes #12183) Files: src/blob.c, src/dict.c, src/list.c, src/proto/blob.pro, src/testdir/test_filter_map.vim *** ../vim-9.0.1415/src/blob.c 2022-11-02 13:30:37.526314510 +0000 --- src/blob.c 2023-03-19 21:20:55.805760032 +0000 *************** *** 592,600 **** blob_T *blob_arg, filtermap_T filtermap, typval_T *expr, typval_T *rettv) { ! blob_T *b; int i; typval_T tv; varnumber_T val; --- 592,601 ---- blob_T *blob_arg, filtermap_T filtermap, typval_T *expr, + char_u *arg_errmsg, typval_T *rettv) { ! blob_T *b = blob_arg; int i; typval_T tv; varnumber_T val; *************** *** 609,615 **** rettv->v_type = VAR_BLOB; rettv->vval.v_blob = NULL; } ! if ((b = blob_arg) == NULL) return; b_ret = b; --- 610,617 ---- rettv->v_type = VAR_BLOB; rettv->vval.v_blob = NULL; } ! if (b == NULL || (filtermap == FILTERMAP_FILTER ! && value_check_lock(b->bv_lock, arg_errmsg, TRUE))) return; b_ret = b; *************** *** 623,628 **** --- 625,634 ---- // set_vim_var_nr() doesn't set the type set_vim_var_type(VV_KEY, VAR_NUMBER); + int prev_lock = b->bv_lock; + if (b->bv_lock == 0) + b->bv_lock = VAR_LOCKED; + // Create one funccal_T for all eval_expr_typval() calls. fc = eval_expr_get_funccal(expr, &newtv); *************** *** 658,663 **** --- 664,670 ---- ++idx; } + b->bv_lock = prev_lock; if (fc != NULL) remove_funccal(); } *** ../vim-9.0.1415/src/dict.c 2023-03-07 17:13:47.305107775 +0000 --- src/dict.c 2023-03-19 21:22:10.809777612 +0000 *************** *** 1305,1311 **** action = (char_u *)"force"; if (type != NULL && check_typval_arg_type(type, &argvars[1], ! func_name, 2) == FAIL) return; dict_extend(d1, d2, action, func_name); --- 1305,1311 ---- action = (char_u *)"force"; if (type != NULL && check_typval_arg_type(type, &argvars[1], ! func_name, 2) == FAIL) return; dict_extend(d1, d2, action, func_name); *************** *** 1333,1339 **** typval_T *expr, typval_T *rettv) { - int prev_lock; dict_T *d_ret = NULL; hashtab_T *ht; hashitem_T *hi; --- 1333,1338 ---- *************** *** 1353,1360 **** && value_check_lock(d->dv_lock, arg_errmsg, TRUE))) return; - prev_lock = d->dv_lock; - if (filtermap == FILTERMAP_MAPNEW) { if (rettv_dict_alloc(rettv) == FAIL) --- 1352,1357 ---- *************** *** 1365,1371 **** // Create one funccal_T for all eval_expr_typval() calls. fc = eval_expr_get_funccal(expr, &newtv); ! if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0) d->dv_lock = VAR_LOCKED; ht = &d->dv_hashtab; hash_lock(ht); --- 1362,1369 ---- // Create one funccal_T for all eval_expr_typval() calls. fc = eval_expr_get_funccal(expr, &newtv); ! int prev_lock = d->dv_lock; ! if (d->dv_lock == 0) d->dv_lock = VAR_LOCKED; ht = &d->dv_hashtab; hash_lock(ht); *** ../vim-9.0.1415/src/list.c 2023-03-09 22:06:45.903407145 +0000 --- src/list.c 2023-03-19 21:18:11.649718359 +0000 *************** *** 2398,2404 **** // set_vim_var_nr() doesn't set the type set_vim_var_type(VV_KEY, VAR_NUMBER); ! if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0) l->lv_lock = VAR_LOCKED; // Create one funccal_T for all eval_expr_typval() calls. --- 2398,2404 ---- // set_vim_var_nr() doesn't set the type set_vim_var_type(VV_KEY, VAR_NUMBER); ! if (l->lv_lock == 0) l->lv_lock = VAR_LOCKED; // Create one funccal_T for all eval_expr_typval() calls. *************** *** 2576,2590 **** if (argvars[0].v_type == VAR_DICT) dict_filter_map(argvars[0].vval.v_dict, filtermap, type, func_name, ! arg_errmsg, expr, rettv); else if (argvars[0].v_type == VAR_BLOB) ! blob_filter_map(argvars[0].vval.v_blob, filtermap, expr, rettv); else if (argvars[0].v_type == VAR_STRING) ! string_filter_map(tv_get_string(&argvars[0]), filtermap, expr, ! rettv); else // argvars[0].v_type == VAR_LIST list_filter_map(argvars[0].vval.v_list, filtermap, type, func_name, ! arg_errmsg, expr, rettv); restore_vimvar(VV_KEY, &save_key); restore_vimvar(VV_VAL, &save_val); --- 2576,2590 ---- if (argvars[0].v_type == VAR_DICT) dict_filter_map(argvars[0].vval.v_dict, filtermap, type, func_name, ! arg_errmsg, expr, rettv); else if (argvars[0].v_type == VAR_BLOB) ! blob_filter_map(argvars[0].vval.v_blob, filtermap, expr, ! arg_errmsg, rettv); else if (argvars[0].v_type == VAR_STRING) ! string_filter_map(tv_get_string(&argvars[0]), filtermap, expr, rettv); else // argvars[0].v_type == VAR_LIST list_filter_map(argvars[0].vval.v_list, filtermap, type, func_name, ! arg_errmsg, expr, rettv); restore_vimvar(VV_KEY, &save_key); restore_vimvar(VV_VAL, &save_val); *** ../vim-9.0.1415/src/proto/blob.pro 2022-10-20 13:28:43.777615365 +0100 --- src/proto/blob.pro 2023-03-19 21:21:15.305764679 +0000 *************** *** 20,26 **** int blob_set_range(blob_T *dest, long n1, long n2, typval_T *src); void blob_add(typval_T *argvars, typval_T *rettv); void blob_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg); ! void blob_filter_map(blob_T *blob_arg, filtermap_T filtermap, typval_T *expr, typval_T *rettv); void blob_insert_func(typval_T *argvars, typval_T *rettv); void blob_reduce(typval_T *argvars, typval_T *expr, typval_T *rettv); void blob_reverse(blob_T *b, typval_T *rettv); --- 20,26 ---- int blob_set_range(blob_T *dest, long n1, long n2, typval_T *src); void blob_add(typval_T *argvars, typval_T *rettv); void blob_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg); ! void blob_filter_map(blob_T *blob_arg, filtermap_T filtermap, typval_T *expr, char_u *arg_errmsg, typval_T *rettv); void blob_insert_func(typval_T *argvars, typval_T *rettv); void blob_reduce(typval_T *argvars, typval_T *expr, typval_T *rettv); void blob_reverse(blob_T *b, typval_T *rettv); *** ../vim-9.0.1415/src/testdir/test_filter_map.vim 2022-01-29 20:30:19.000000000 +0000 --- src/testdir/test_filter_map.vim 2023-03-19 21:12:24.001608481 +0000 *************** *** 116,121 **** --- 116,136 ---- let d = #{a: 1, b: 2, c: 3} call assert_fails('call map(d, "remove(d, v:key)[0]")', 'E741:') call assert_fails('echo map(d, {k,v -> remove(d, k)})', 'E741:') + + let b = 0z1234 + call assert_fails('call filter(b, "remove(b, 0)")', 'E741:') + endfunc + + func Test_filter_and_modify() + let l = [0] + " cannot change the list halfway a map() + call assert_fails('call filter(l, "remove(l, 0)")', 'E741:') + + let d = #{a: 0, b: 0, c: 0} + call assert_fails('call filter(d, "remove(d, v:key)")', 'E741:') + + let b = 0z1234 + call assert_fails('call filter(b, "remove(b, 0)")', 'E741:') endfunc func Test_mapnew_dict() *** ../vim-9.0.1415/src/version.c 2023-03-19 20:43:21.925113688 +0000 --- src/version.c 2023-03-19 21:14:29.345652389 +0000 *************** *** 697,698 **** --- 697,700 ---- { /* Add new patch number below this line */ + /**/ + 1416, /**/ -- GALAHAD hurries to the door and pushes through it. As he leaves the room we CUT TO the reverse to show that he is now in a room full of bathing and romping GIRLIES, all innocent, wide-eyed and beautiful. They smile enchantingly at him as he tries to keep walking without being diverted by the lovely sights assaulting his eyeballs. "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///