To: vim_dev@googlegroups.com Subject: Patch 9.0.0117 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.0117 Problem: Text of removed textprop with text is not freed. Solution: Free the text when the property is removed. Reduce the array size to ignore NULLs at the end. Files: src/textprop.c, src/testdir/test_textprop.vim *** ../vim-9.0.0116/src/textprop.c 2022-07-25 18:13:33.054580717 +0100 --- src/textprop.c 2022-07-30 21:55:30.664573824 +0100 *************** *** 1233,1241 **** dict_T *dict; buf_T *buf = curbuf; int do_all; ! int id = -1; int type_id = -1; int both; rettv->vval.v_number = 0; --- 1233,1242 ---- dict_T *dict; buf_T *buf = curbuf; int do_all; ! int id = -MAXCOL; int type_id = -1; int both; + int did_remove_text = FALSE; rettv->vval.v_number = 0; *************** *** 1286,1297 **** } both = dict_get_bool(dict, "both", FALSE); ! if (id == -1 && type_id == -1) { emsg(_(e_need_at_least_one_of_id_or_type)); return; } ! if (both && (id == -1 || type_id == -1)) { emsg(_(e_need_id_and_type_with_both)); return; --- 1287,1298 ---- } both = dict_get_bool(dict, "both", FALSE); ! if (id == -MAXCOL && type_id == -1) { emsg(_(e_need_at_least_one_of_id_or_type)); return; } ! if (both && (id == -MAXCOL || type_id == -1)) { emsg(_(e_need_id_and_type_with_both)); return; *************** *** 1350,1355 **** --- 1351,1371 ---- buf->b_ml.ml_line_len -= sizeof(textprop_T); --idx; + if (textprop.tp_id < 0) + { + garray_T *gap = &buf->b_textprop_text; + int ii = -textprop.tp_id - 1; + + // negative ID: property with text - free the text + if (ii < gap->ga_len) + { + char_u **p = ((char_u **)gap->ga_data) + ii; + vim_free(*p); + *p = NULL; + did_remove_text = TRUE; + } + } + if (first_changed == 0) first_changed = lnum; last_changed = lnum; *************** *** 1360,1370 **** --- 1376,1397 ---- } } } + if (first_changed > 0) { changed_lines_buf(buf, first_changed, last_changed + 1, 0); redraw_buf_later(buf, VALID); } + + if (did_remove_text) + { + garray_T *gap = &buf->b_textprop_text; + + // Reduce the growarray size for NULL pointers at the end. + while (gap->ga_len > 0 + && ((char_u **)gap->ga_data)[gap->ga_len - 1] == NULL) + --gap->ga_len; + } } /* *** ../vim-9.0.0116/src/testdir/test_textprop.vim 2022-07-30 21:33:18.200195441 +0100 --- src/testdir/test_textprop.vim 2022-07-30 22:07:31.875995956 +0100 *************** *** 2213,2216 **** --- 2213,2235 ---- call delete('XscriptPropsWithText') endfunc + func Test_removed_prop_with_text_cleans_up_array() + new + call setline(1, 'some text here') + call prop_type_add('some', #{highlight: 'ErrorMsg'}) + let id1 = prop_add(1, 5, #{type: 'some', text: "SOME"}) + call assert_equal(-1, id1) + let id2 = prop_add(1, 10, #{type: 'some', text: "HERE"}) + call assert_equal(-2, id2) + + " removing the props resets the index + call prop_remove(#{id: id1}) + call prop_remove(#{id: id2}) + let id1 = prop_add(1, 5, #{type: 'some', text: "SOME"}) + call assert_equal(-1, id1) + + call prop_type_delete('some') + bwipe! + endfunc + " vim: shiftwidth=2 sts=2 expandtab *** ../vim-9.0.0116/src/version.c 2022-07-30 21:33:18.200195441 +0100 --- src/version.c 2022-07-30 22:06:36.331872578 +0100 *************** *** 737,738 **** --- 737,740 ---- { /* Add new patch number below this line */ + /**/ + 117, /**/ -- Spam seems to be something useful to novices. Later you realize that it's a bunch of indigestable junk that only clogs your system. Applies to both the food and the e-mail! /// 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 ///