vim-patch:8.2.2664: Vim9: not enough function arguments checked for string

Problem:    Vim9: not enough function arguments checked for string.
Solution:   Check in balloon functions.  Refactor function arguments.
32105ae88f

Cherry-pick removal of useless check from patch 8.2.3840.

vim-patch:8.2.3083: crash when passing null string to charclass()

Problem:    Crash when passing null string to charclass().
Solution:   Bail out when string pointer is NULL. (Christian Brabandt,
            closes vim/vim#8498, closes vim/vim#8260)
72463f883c
This commit is contained in:
zeertzjq 2022-09-12 14:08:51 +08:00
parent 38059b4f31
commit 49aa9e17fa
3 changed files with 12 additions and 20 deletions

View File

@ -1772,7 +1772,7 @@ static void f_eventhandler(typval_T *argvars, typval_T *rettv, EvalFuncData fptr
/// "executable()" function /// "executable()" function
static void f_executable(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) static void f_executable(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{ {
if (tv_check_for_string(&argvars[0], 1) == FAIL) { if (tv_check_for_string_arg(argvars, 0) == FAIL) {
return; return;
} }
@ -1901,7 +1901,7 @@ static void f_win_execute(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
/// "exepath()" function /// "exepath()" function
static void f_exepath(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) static void f_exepath(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{ {
if (tv_check_for_nonempty_string(&argvars[0], 1) == FAIL) { if (tv_check_for_nonempty_string_arg(argvars, 0) == FAIL) {
return; return;
} }

View File

@ -3805,34 +3805,26 @@ float_T tv_get_float(const typval_T *const tv)
return 0; return 0;
} }
/// Give an error and return FAIL unless "tv" is a string. /// Give an error and return FAIL unless "args[idx]" is a string.
int tv_check_for_string(const typval_T *const tv, const int arg) int tv_check_for_string_arg(const typval_T *const args, const int idx)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
{ {
if (tv->v_type != VAR_STRING) { if (args[idx].v_type != VAR_STRING) {
if (arg > 0) { semsg(_(e_string_required_for_argument_nr), idx + 1);
semsg(_(e_string_required_for_argument_nr), arg);
} else {
emsg(_(e_stringreq));
}
return FAIL; return FAIL;
} }
return OK; return OK;
} }
/// Give an error and return FAIL unless "tv" is a non-empty string. /// Give an error and return FAIL unless "args[idx]" is a non-empty string.
int tv_check_for_nonempty_string(const typval_T *const tv, const int arg) int tv_check_for_nonempty_string_arg(const typval_T *const args, const int idx)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE
{ {
if (tv_check_for_string(tv, arg) == FAIL) { if (tv_check_for_string_arg(args, idx) == FAIL) {
return FAIL; return FAIL;
} }
if (tv->vval.v_string == NULL || *tv->vval.v_string == NUL) { if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL) {
if (arg > 0) { semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
semsg(_(e_non_empty_string_required_for_argument_nr), arg);
} else {
emsg(_(e_non_empty_string_required));
}
return FAIL; return FAIL;
} }
return OK; return OK;

View File

@ -2793,7 +2793,7 @@ void f_setcellwidths(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
void f_charclass(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) void f_charclass(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{ {
if (tv_check_for_string(&argvars[0], 1) == FAIL if (tv_check_for_string_arg(argvars, 0) == FAIL
|| argvars[0].vval.v_string == NULL) { || argvars[0].vval.v_string == NULL) {
return; return;
} }