changeset 23909:5db7d275543c v8.2.2497

patch 8.2.2497: no error when using more than one character for a register Commit: https://github.com/vim/vim/commit/418a29f0ffcaa0a3d778724ab6d1111db525d3cc Author: Bram Moolenaar <Bram@vim.org> Date: Wed Feb 10 22:23:41 2021 +0100 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 https://github.com/vim/vim/issues/7814) Fix that VAR_BOOL and VAR_SPECIAL are not considered equal.
author Bram Moolenaar <Bram@vim.org>
date Wed, 10 Feb 2021 22:30:06 +0100
parents 0b3a5d2eff3d
children 6cfb5e0231e0
files src/errors.h src/evalfunc.c src/testdir/test_vim9_builtin.vim src/typval.c src/version.c
diffstat 5 files changed, 46 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/errors.h
+++ b/src/errors.h
@@ -359,3 +359,5 @@ EXTERN char e_cannot_use_default_for_var
 	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"));
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -4285,7 +4285,13 @@ f_getreg(typval_T *argvars, typval_T *re
     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
     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
 	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
 	{
 	    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 *re
 
     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 = '"';
--- 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<string> = 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()
--- 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)
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    2497,
+/**/
     2496,
 /**/
     2495,