changeset 32385:49a13ac4a1b1 v9.0.1524

patch 9.0.1524: passing -1 for bool is not always rejected Commit: https://github.com/vim/vim/commit/8cf51376b842e0060edf08bd2e5bd9933c552ecf Author: zeertzjq <zeertzjq@outlook.com> Date: Mon May 8 15:31:38 2023 +0100 patch 9.0.1524: passing -1 for bool is not always rejected Problem: Passing -1 for bool is not always rejected. Solution: Check for error in a better way. (closes https://github.com/vim/vim/issues/12358)
author Bram Moolenaar <Bram@vim.org>
date Mon, 08 May 2023 16:45:03 +0200
parents 0843fba7af86
children 1714bba63e87
files src/strings.c src/testdir/test_expr.vim src/testdir/test_functions.vim src/testdir/test_utf8.vim src/version.c
diffstat 5 files changed, 29 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/strings.c
+++ b/src/strings.c
@@ -1065,11 +1065,13 @@ byteidx_common(typval_T *argvars, typval
     varnumber_T	utf16idx = FALSE;
     if (argvars[2].v_type != VAR_UNKNOWN)
     {
-	utf16idx = tv_get_bool(&argvars[2]);
+	int error = FALSE;
+	utf16idx = tv_get_bool_chk(&argvars[2], &error);
+	if (error)
+	    return;
 	if (utf16idx < 0 || utf16idx > 1)
 	{
-	    if (utf16idx != -1)
-		semsg(_(e_using_number_as_bool_nr), utf16idx);
+	    semsg(_(e_using_number_as_bool_nr), utf16idx);
 	    return;
 	}
     }
@@ -1421,14 +1423,19 @@ f_strchars(typval_T *argvars, typval_T *
 	return;
 
     if (argvars[1].v_type != VAR_UNKNOWN)
-	skipcc = tv_get_bool(&argvars[1]);
-    if (skipcc < 0 || skipcc > 1)
     {
-	if (skipcc != -1)
+	int error = FALSE;
+	skipcc = tv_get_bool_chk(&argvars[1], &error);
+	if (error)
+	    return;
+	if (skipcc < 0 || skipcc > 1)
+	{
 	    semsg(_(e_using_number_as_bool_nr), skipcc);
+	    return;
+	}
     }
-    else
-	strchar_common(argvars, rettv, skipcc);
+
+    strchar_common(argvars, rettv, skipcc);
 }
 
 /*
@@ -1533,11 +1540,12 @@ f_strcharpart(typval_T *argvars, typval_
 	if (argvars[2].v_type != VAR_UNKNOWN
 					   && argvars[3].v_type != VAR_UNKNOWN)
 	{
-	    skipcc = tv_get_bool(&argvars[3]);
+	    skipcc = tv_get_bool_chk(&argvars[3], &error);
+	    if (error)
+		return;
 	    if (skipcc < 0 || skipcc > 1)
 	    {
-		if (skipcc != -1)
-		    semsg(_(e_using_number_as_bool_nr), skipcc);
+		semsg(_(e_using_number_as_bool_nr), skipcc);
 		return;
 	    }
 	}
--- a/src/testdir/test_expr.vim
+++ b/src/testdir/test_expr.vim
@@ -161,7 +161,8 @@ func Test_strcharpart()
   END
   call v9.CheckLegacyAndVim9Success(lines)
 
-  call assert_fails('echo strcharpart("", 0, 0, {})', ['E728:', 'E728:'])
+  call assert_fails('call strcharpart("", 0, 0, {})', ['E728:', 'E728:'])
+  call assert_fails('call strcharpart("", 0, 0, -1)', ['E1023:', 'E1023:'])
 endfunc
 
 func Test_getreg_empty_list()
--- a/src/testdir/test_functions.vim
+++ b/src/testdir/test_functions.vim
@@ -1225,6 +1225,7 @@ func Test_byteidx()
   call assert_fails("call byteidx([], 0)", 'E730:')
   call assert_fails("call byteidx('abc', [])", 'E745:')
   call assert_fails("call byteidx('abc', 0, {})", ['E728:', 'E728:'])
+  call assert_fails("call byteidx('abc', 0, -1)", ['E1023:', 'E1023:'])
 endfunc
 
 " Test for byteidxcomp() using a character index
@@ -1265,6 +1266,7 @@ func Test_byteidxcomp()
   call assert_fails("call byteidxcomp([], 0)", 'E730:')
   call assert_fails("call byteidxcomp('abc', [])", 'E745:')
   call assert_fails("call byteidxcomp('abc', 0, {})", ['E728:', 'E728:'])
+  call assert_fails("call byteidxcomp('abc', 0, -1)", ['E1023:', 'E1023:'])
 endfunc
 
 " Test for byteidx() using a UTF-16 index
@@ -1625,7 +1627,6 @@ func Test_utf16idx_from_charidx()
   " error cases
   call assert_equal(-1, utf16idx(test_null_string(), 0, v:true, v:true))
   call assert_fails('let l = utf16idx("ab", 0, v:false, [])', 'E1212:')
-  call assert_fails('echo strchars("", {})', ['E728:', 'E728:'])
 endfunc
 
 " Test for strutf16len()
--- a/src/testdir/test_utf8.vim
+++ b/src/testdir/test_utf8.vim
@@ -29,8 +29,10 @@ func Test_strchars()
     call assert_equal(exp[i], strcharlen(inp[i]))
   endfor
 
-  call assert_fails("let v=strchars('abc', [])", 'E745:')
-  call assert_fails("let v=strchars('abc', 2)", 'E1023:')
+  call assert_fails("call strchars('abc', 2)", ['E1023:', 'E1023:'])
+  call assert_fails("call strchars('abc', -1)", ['E1023:', 'E1023:'])
+  call assert_fails("call strchars('abc', {})", ['E728:', 'E728:'])
+  call assert_fails("call strchars('abc', [])", ['E745:', 'E745:'])
 endfunc
 
 " Test for customlist completion
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1524,
+/**/
     1523,
 /**/
     1522,