From 232cdbb8807c79a5d933f28345011d2a32264f02 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 22 Jul 2022 21:47:03 +0800 Subject: vim-patch:8.2.1489: Vim9: error when setting an option with setbufvar() Problem: Vim9: error when setting an option with setbufvar(). Solution: Do not get a number from a string value. (closes vim/vim#6740) https://github.com/vim/vim/commit/191929b182ba38abe6bc431fb9d8d9507f408903 Vim9 is N/A, so this just refactors the code without changing behavior. --- src/nvim/eval/vars.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/nvim/eval/vars.c b/src/nvim/eval/vars.c index 01fd10dcda..e56ccef02b 100644 --- a/src/nvim/eval/vars.c +++ b/src/nvim/eval/vars.c @@ -1544,6 +1544,18 @@ static void getwinvar(typval_T *argvars, typval_T *rettv, int off) } } +/// Set option "varname" to the value of "varp" for the current buffer/window. +static void set_option_from_tv(const char *varname, typval_T *varp) +{ + bool error = false; + char nbuf[NUMBUFLEN]; + const long numval = (long)tv_get_number_chk(varp, &error); + const char *const strval = tv_get_string_buf_chk(varp, nbuf); + if (!error && strval != NULL) { + set_option_value(varname, numval, strval, OPT_LOCAL); + } +} + /// "setwinvar()" and "settabwinvar()" functions static void setwinvar(typval_T *argvars, typval_T *rettv, int off) { @@ -1566,16 +1578,7 @@ static void setwinvar(typval_T *argvars, typval_T *rettv, int off) switchwin_T switchwin; if (!need_switch_win || switch_win(&switchwin, win, tp, true) == OK) { if (*varname == '&') { - long numval; - bool error = false; - - varname++; - numval = tv_get_number_chk(varp, &error); - char nbuf[NUMBUFLEN]; - const char *const strval = tv_get_string_buf_chk(varp, nbuf); - if (!error && strval != NULL) { - set_option_value(varname, numval, strval, OPT_LOCAL); - } + set_option_from_tv(varname + 1, varp); } else { const size_t varname_len = strlen(varname); char *const winvarname = xmalloc(varname_len + 3); @@ -1789,20 +1792,12 @@ void f_setbufvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) if (buf != NULL && varname != NULL) { if (*varname == '&') { - long numval; - bool error = false; aco_save_T aco; // set curbuf to be our buf, temporarily aucmd_prepbuf(&aco, buf); - varname++; - numval = tv_get_number_chk(varp, &error); - char nbuf[NUMBUFLEN]; - const char *const strval = tv_get_string_buf_chk(varp, nbuf); - if (!error && strval != NULL) { - set_option_value(varname, numval, strval, OPT_LOCAL); - } + set_option_from_tv(varname + 1, varp); // reset notion of buffer aucmd_restbuf(&aco); -- cgit