From 418a29f0ffcaa0a3d778724ab6d1111db525d3cc Mon Sep 17 00:00:00 2001 From: Bram Moolenaar Date: Wed, 10 Feb 2021 22:23:41 +0100 Subject: patch 8.2.2497: no error when using more than one character for a register Problem: No error when using more than one character for a register name. Solution: In Vim9 script check for a single character string. (closes #7814) Fix that VAR_BOOL and VAR_SPECIAL are not considered equal. --- src/errors.h | 2 ++ src/evalfunc.c | 25 +++++++++++++++++++++++-- src/testdir/test_vim9_builtin.vim | 16 ++++++++++++++++ src/typval.c | 4 +++- src/version.c | 2 ++ 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/errors.h b/src/errors.h index aaf7e552c..39eca7e94 100644 --- a/src/errors.h +++ b/src/errors.h @@ -359,3 +359,5 @@ EXTERN char e_cannot_use_default_for_variable_arguments[] INIT(= N_("E1160: Cannot use a default for variable arguments")); EXTERN char e_cannot_json_encode_str[] INIT(= N_("E1161: Cannot json encode a %s")); +EXTERN char e_register_name_must_be_one_char_str[] + INIT(= N_("E1162: Register name must be one character: %s")); diff --git a/src/evalfunc.c b/src/evalfunc.c index 01994b047..b3da9f6cf 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -4285,7 +4285,13 @@ f_getreg(typval_T *argvars, typval_T *rettv) if (argvars[0].v_type != VAR_UNKNOWN) { strregname = tv_get_string_chk(&argvars[0]); - error = strregname == NULL; + if (strregname == NULL) + error = TRUE; + else if (in_vim9script() && STRLEN(strregname) > 1) + { + semsg(_(e_register_name_must_be_one_char_str), strregname); + error = TRUE; + } if (argvars[1].v_type != VAR_UNKNOWN) { arg2 = (int)tv_get_bool_chk(&argvars[1], &error); @@ -4335,6 +4341,11 @@ f_getregtype(typval_T *argvars, typval_T *rettv) if (argvars[0].v_type != VAR_UNKNOWN) { strregname = tv_get_string_chk(&argvars[0]); + if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1) + { + semsg(_(e_register_name_must_be_one_char_str), strregname); + strregname = NULL; + } if (strregname == NULL) // type error; errmsg already given { rettv->v_type = VAR_STRING; @@ -7368,6 +7379,11 @@ f_getreginfo(typval_T *argvars, typval_T *rettv) strregname = tv_get_string_chk(&argvars[0]); if (strregname == NULL) return; + if (in_vim9script() && STRLEN(strregname) > 1) + { + semsg(_(e_register_name_must_be_one_char_str), strregname); + return; + } } else strregname = get_vim_var_str(VV_REG); @@ -7410,7 +7426,7 @@ f_getreginfo(typval_T *argvars, typval_T *rettv) { item->di_tv.v_type = VAR_SPECIAL; item->di_tv.vval.v_number = regname == buf[0] - ? VVAL_TRUE : VVAL_FALSE; + ? VVAL_TRUE : VVAL_FALSE; (void)dict_add(dict, item); } } @@ -8472,6 +8488,11 @@ f_setreg(typval_T *argvars, typval_T *rettv) if (strregname == NULL) return; // type error; errmsg already given + if (in_vim9script() && STRLEN(strregname) > 1) + { + semsg(_(e_register_name_must_be_one_char_str), strregname); + return; + } regname = *strregname; if (regname == 0 || regname == '@') regname = '"'; diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim index eb36bd77a..cb3699065 100644 --- a/src/testdir/test_vim9_builtin.vim +++ b/src/testdir/test_vim9_builtin.vim @@ -531,6 +531,7 @@ def Test_getreg() var lines = ['aaa', 'bbb', 'ccc'] setreg('a', lines) getreg('a', true, true)->assert_equal(lines) + assert_fails('getreg("ab")', 'E1162:') enddef def Test_getreg_return_type() @@ -539,6 +540,20 @@ def Test_getreg_return_type() var s3: list = getreg('"', 1, 1) enddef +def Test_getreginfo() + var text = 'abc' + setreg('a', text) + getreginfo('a')->assert_equal({regcontents: [text], regtype: 'v', isunnamed: false}) + assert_fails('getreginfo("ab")', 'E1162:') +enddef + +def Test_getregtype() + var lines = ['aaa', 'bbb', 'ccc'] + setreg('a', lines) + getregtype('a')->assert_equal('V') + assert_fails('getregtype("ab")', 'E1162:') +enddef + def Test_glob() glob('runtest.vim', true, true, true)->assert_equal(['runtest.vim']) enddef @@ -878,6 +893,7 @@ def Test_setreg() var reginfo = getreginfo('a') setreg('a', reginfo) getreginfo('a')->assert_equal(reginfo) + assert_fails('setreg("ab", 0)', 'E1162:') enddef def Test_slice() diff --git a/src/typval.c b/src/typval.c index 65e07b3f7..1cdab93ef 100644 --- a/src/typval.c +++ b/src/typval.c @@ -1069,7 +1069,9 @@ tv_equal( return r; } - if (tv1->v_type != tv2->v_type) + if (tv1->v_type != tv2->v_type + && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL) + || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL))) return FALSE; switch (tv1->v_type) diff --git a/src/version.c b/src/version.c index 3c9edfdce..55dd8d164 100644 --- a/src/version.c +++ b/src/version.c @@ -750,6 +750,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 2497, /**/ 2496, /**/ -- cgit v1.2.1