summaryrefslogtreecommitdiff
path: root/src/strings.c
diff options
context:
space:
mode:
authorYegappan Lakshmanan <yegappan@yahoo.com>2021-07-27 22:00:44 +0200
committerBram Moolenaar <Bram@vim.org>2021-07-27 22:00:44 +0200
commit4490ec4e839e45a2e6923c265c7e9e64c240b805 (patch)
tree3ef2dc127890ac6a644f38ae7932b7e70071544a /src/strings.c
parent5d7c2df536c17db4a9c61e0760bdcf78d0db7330 (diff)
downloadvim-git-4490ec4e839e45a2e6923c265c7e9e64c240b805.tar.gz
patch 8.2.3229: Vim9: runtime and compile time type checks are not the samev8.2.3229
Problem: Vim9: runtime and compile time type checks are not the same. Solution: Add more runtime type checks for builtin functions. (Yegappan Lakshmanan, closes #8646)
Diffstat (limited to 'src/strings.c')
-rw-r--r--src/strings.c65
1 files changed, 63 insertions, 2 deletions
diff --git a/src/strings.c b/src/strings.c
index bc0fc5e31..f4b83d908 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -853,6 +853,12 @@ f_charidx(typval_T *argvars, typval_T *rettv)
rettv->vval.v_number = -1;
+ if (in_vim9script()
+ && (check_for_string_arg(argvars, 0) == FAIL
+ || check_for_number_arg(argvars, 1) == FAIL
+ || check_for_opt_bool_arg(argvars, 2) == FAIL))
+ return;
+
if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
|| (argvars[2].v_type != VAR_UNKNOWN
&& argvars[2].v_type != VAR_NUMBER
@@ -948,6 +954,13 @@ f_str2nr(typval_T *argvars, typval_T *rettv)
int what = 0;
int isneg;
+ if (in_vim9script()
+ && (check_for_string_arg(argvars, 0) == FAIL
+ || check_for_opt_number_arg(argvars, 1) == FAIL
+ || (argvars[1].v_type != VAR_UNKNOWN
+ && check_for_opt_bool_arg(argvars, 2) == FAIL)))
+ return;
+
if (argvars[1].v_type != VAR_UNKNOWN)
{
base = (int)tv_get_number(&argvars[1]);
@@ -1031,6 +1044,12 @@ f_stridx(typval_T *argvars, typval_T *rettv)
char_u *pos;
int start_idx;
+ if (in_vim9script()
+ && (check_for_string_arg(argvars, 0) == FAIL
+ || check_for_string_arg(argvars, 1) == FAIL
+ || check_for_opt_number_arg(argvars, 2) == FAIL))
+ return;
+
needle = tv_get_string_chk(&argvars[1]);
save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
rettv->vval.v_number = -1;
@@ -1076,6 +1095,10 @@ f_string(typval_T *argvars, typval_T *rettv)
void
f_strlen(typval_T *argvars, typval_T *rettv)
{
+ if (in_vim9script()
+ && check_for_string_or_number_arg(argvars, 0) == FAIL)
+ return;
+
rettv->vval.v_number = (varnumber_T)(STRLEN(
tv_get_string(&argvars[0])));
}
@@ -1102,6 +1125,10 @@ strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
void
f_strcharlen(typval_T *argvars, typval_T *rettv)
{
+ if (in_vim9script()
+ && check_for_string_or_number_arg(argvars, 0) == FAIL)
+ return;
+
strchar_common(argvars, rettv, TRUE);
}
@@ -1155,8 +1182,12 @@ f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
void
f_strwidth(typval_T *argvars, typval_T *rettv)
{
- char_u *s = tv_get_string_strict(&argvars[0]);
+ char_u *s;
+ if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
+ return;
+
+ s = tv_get_string_strict(&argvars[0]);
rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
}
@@ -1326,6 +1357,12 @@ f_strridx(typval_T *argvars, typval_T *rettv)
char_u *lastmatch = NULL;
int haystack_len, end_idx;
+ if (in_vim9script()
+ && (check_for_string_arg(argvars, 0) == FAIL
+ || check_for_string_arg(argvars, 1) == FAIL
+ || check_for_opt_number_arg(argvars, 2) == FAIL))
+ return;
+
needle = tv_get_string_chk(&argvars[1]);
haystack = tv_get_string_buf_chk(&argvars[0], buf);
@@ -1372,6 +1409,9 @@ f_strridx(typval_T *argvars, typval_T *rettv)
void
f_strtrans(typval_T *argvars, typval_T *rettv)
{
+ if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
+ return;
+
rettv->v_type = VAR_STRING;
rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
}
@@ -1382,6 +1422,9 @@ f_strtrans(typval_T *argvars, typval_T *rettv)
void
f_tolower(typval_T *argvars, typval_T *rettv)
{
+ if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
+ return;
+
rettv->v_type = VAR_STRING;
rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
}
@@ -1392,6 +1435,9 @@ f_tolower(typval_T *argvars, typval_T *rettv)
void
f_toupper(typval_T *argvars, typval_T *rettv)
{
+ if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
+ return;
+
rettv->v_type = VAR_STRING;
rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
}
@@ -1417,6 +1463,12 @@ f_tr(typval_T *argvars, typval_T *rettv)
char_u buf2[NUMBUFLEN];
garray_T ga;
+ if (in_vim9script()
+ && (check_for_string_arg(argvars, 0) == FAIL
+ || check_for_string_arg(argvars, 1) == FAIL
+ || check_for_string_arg(argvars, 2) == FAIL))
+ return;
+
in_str = tv_get_string(&argvars[0]);
fromstr = tv_get_string_buf_chk(&argvars[1], buf);
tostr = tv_get_string_buf_chk(&argvars[2], buf2);
@@ -1517,7 +1569,7 @@ f_trim(typval_T *argvars, typval_T *rettv)
{
char_u buf1[NUMBUFLEN];
char_u buf2[NUMBUFLEN];
- char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
+ char_u *head;
char_u *mask = NULL;
char_u *tail;
char_u *prev;
@@ -1527,6 +1579,15 @@ f_trim(typval_T *argvars, typval_T *rettv)
rettv->v_type = VAR_STRING;
rettv->vval.v_string = NULL;
+
+ if (in_vim9script()
+ && (check_for_string_arg(argvars, 0) == FAIL
+ || check_for_opt_string_arg(argvars, 1) == FAIL
+ || (argvars[1].v_type != VAR_UNKNOWN
+ && check_for_opt_number_arg(argvars, 2) == FAIL)))
+ return;
+
+ head = tv_get_string_buf_chk(&argvars[0], buf1);
if (head == NULL)
return;