From 3d93e47d9af5722c0e1be1d073090347d6ae28e3 Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Fri, 5 Sep 2014 13:15:48 -0400 Subject: [PATCH] vim-patch:7.4.249 Problem: Using setreg() with a list of numbers does not work. Solution: Use a separate buffer for numbers. (ZyX) https://code.google.com/p/vim/source/detail?r=v7-4-249 --- src/nvim/eval.c | 23 +++++++++++++++++++---- src/nvim/testdir/test_eval.in | 6 ++++-- src/nvim/testdir/test_eval.ok | Bin 10307 -> 10578 bytes src/nvim/version.c | 2 +- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 2e2871abc5..86ac112212 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -13373,22 +13373,36 @@ static void f_setreg(typval_T *argvars, typval_T *rettv) if (argvars[1].v_type == VAR_LIST) { int len = argvars[1].vval.v_list->lv_len; - char_u **lstval = xmalloc(sizeof(char_u *) * (len + 1)); + // First half: use for pointers to result lines; second half: use for + // pointers to allocated copies. + char_u **lstval = xmalloc(sizeof(char_u *) * ((len + 1) * 2)); char_u **curval = lstval; + char_u **allocval = lstval + len + 2; + char_u **curallocval = allocval; + char_u buf[NUMBUFLEN]; for (listitem_T *li = argvars[1].vval.v_list->lv_first; li != NULL; li = li->li_next) { - char_u *strval = get_tv_string_chk(&li->li_tv); + char_u *strval = get_tv_string_buf_chk(&li->li_tv, buf); if (strval == NULL) { - free(lstval); - return; + goto free_lstval; + } + if (strval == buf) { + // Need to make a copy, + // next get_tv_string_buf_chk() will overwrite the string. + strval = vim_strsave(buf); + *curallocval++ = strval; } *curval++ = strval; } *curval++ = NULL; write_reg_contents_lst(regname, lstval, -1, append, yank_type, block_len); + +free_lstval: + while (curallocval > allocval) + free(*--curallocval); free(lstval); } else { char_u *strval = get_tv_string_chk(&argvars[1]); @@ -16334,6 +16348,7 @@ static char_u *get_tv_string_buf(typval_T *varp, char_u *buf) return res != NULL ? res : (char_u *)""; } +/// Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE! char_u *get_tv_string_chk(typval_T *varp) { static char_u mybuf[NUMBUFLEN]; diff --git a/src/nvim/testdir/test_eval.in b/src/nvim/testdir/test_eval.in index 176e62b9cc..95a59ee42a 100644 --- a/src/nvim/testdir/test_eval.in +++ b/src/nvim/testdir/test_eval.in @@ -90,6 +90,8 @@ call SetReg('a', ['abcA3'], 'c') call SetReg('b', ['abcB3'], 'l') call SetReg('c', ['abcC3'], 'b') call SetReg('d', ['abcD3']) +call SetReg('e', [1, 2, 'abc', 3]) +call SetReg('f', [1, 2, 3]) $put ='{{{1 Appending lists with setreg()' call SetReg('A', ['abcA3c'], 'c') @@ -128,8 +130,8 @@ call ErrExe('call setreg(1, 2, 3, 4)') call ErrExe('call setreg([], 2)') call ErrExe('call setreg(1, {})') call ErrExe('call setreg(1, 2, [])') -call ErrExe('call setreg("/", [1, 2])') -call ErrExe('call setreg("=", [1, 2])') +call ErrExe('call setreg("/", ["1", "2"])') +call ErrExe('call setreg("=", ["1", "2"])') call ErrExe('call setreg(1, ["", "", [], ""])') endfun :" diff --git a/src/nvim/testdir/test_eval.ok b/src/nvim/testdir/test_eval.ok index 7fe5f1bd1b2745600d74f4bb8a86e7fa1391f607..061e0cfd2f37ba12d5e66b1fe51221385b899374 100644 GIT binary patch delta 285 zcmX>ca4BemlVH7(LUC$IQEIw|daAmPLbRcdf{~7bdSX&CkY^mL$(3rQP*Pctst{(a zP?nfe3gj9x7%>3l8H^beG@{iFf$G$aKmj0