Proper type checking for set{qf,loc}list()

Prior to this change, type errors were silently ignored. They're explicit now.

  setqflist(list, action, title)
  setloclist(win, list, action, title)

"list" (required) must be a list.
"action" (optional) must a string.
"title" (optional) must a string or number that gets converted to a string.

An error is thrown otherwise.
This commit is contained in:
Marco Hinz 2016-03-02 13:35:01 +01:00
parent 5ef3e40b37
commit 53aa569918

View File

@ -160,6 +160,7 @@ static char *e_listdictarg = N_(
static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary"); static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
static char *e_listreq = N_("E714: List required"); static char *e_listreq = N_("E714: List required");
static char *e_dictreq = N_("E715: Dictionary required"); static char *e_dictreq = N_("E715: Dictionary required");
static char *e_strreq = N_("E114: String required");
static char *e_toomanyarg = N_("E118: Too many arguments for function: %s"); static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
static char *e_dictkey = N_("E716: Key not present in Dictionary: %s"); static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
static char *e_funcexts = N_( static char *e_funcexts = N_(
@ -15291,43 +15292,51 @@ static void f_setline(typval_T *argvars, typval_T *rettv)
/// replace its content or create a new one. /// replace its content or create a new one.
/// @param[in] title_arg New list title. Defaults to caller function name. /// @param[in] title_arg New list title. Defaults to caller function name.
/// @param[out] rettv Return value: 0 in case of success, -1 otherwise. /// @param[out] rettv Return value: 0 in case of success, -1 otherwise.
static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, static void set_qf_ll_list(win_T *wp, typval_T *args, typval_T *rettv)
typval_T *title_arg, typval_T *rettv) FUNC_ATTR_NONNULL_ARG(2, 3)
FUNC_ATTR_NONNULL_ARG(2, 3, 4, 5)
{ {
char_u *act;
int action = ' ';
char_u *title = NULL; char_u *title = NULL;
int action = ' ';
rettv->vval.v_number = -1; rettv->vval.v_number = -1;
if (list_arg->v_type != VAR_LIST) typval_T *list_arg = &args[0];
if (list_arg->v_type != VAR_LIST) {
EMSG(_(e_listreq)); EMSG(_(e_listreq));
else { return;
list_T *l = list_arg->vval.v_list; }
if (action_arg->v_type != VAR_UNKNOWN) { typval_T *action_arg = &args[1];
act = get_tv_string_chk(action_arg); if (action_arg->v_type == VAR_UNKNOWN) {
if (act == NULL) // Option argument was not given.
return; /* type error; errmsg already given */ goto skip_args;
if (*act == 'a' || *act == 'r') } else if (action_arg->v_type != VAR_STRING) {
action = *act; EMSG(_(e_strreq));
} return;
}
char_u *act = get_tv_string_chk(action_arg);
if (*act == 'a' || *act == 'r') {
action = *act;
}
if (title_arg->v_type == VAR_STRING) { typval_T *title_arg = &args[2];
title = get_tv_string_chk(title_arg); if (title_arg->v_type == VAR_UNKNOWN) {
if (!title) { // Option argument was not given.
return; // type error; errmsg already given goto skip_args;
} }
} title = get_tv_string_chk(title_arg);
if (!title) {
// Type error. Error already printed by get_tv_string_chk().
return;
}
if (!title) { skip_args:
title = (char_u*)(wp ? "setloclist()" : "setqflist()"); if (!title) {
} title = (char_u*)(wp ? "setloclist()" : "setqflist()");
}
if (l && set_errorlist(wp, l, action, title) == OK) { list_T *l = list_arg->vval.v_list;
rettv->vval.v_number = 0; if (l && set_errorlist(wp, l, action, title) == OK) {
} rettv->vval.v_number = 0;
} }
} }
@ -15342,7 +15351,7 @@ static void f_setloclist(typval_T *argvars, typval_T *rettv)
win = find_win_by_nr(&argvars[0], NULL); win = find_win_by_nr(&argvars[0], NULL);
if (win != NULL) { if (win != NULL) {
set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv); set_qf_ll_list(win, &argvars[1], rettv);
} }
} }
@ -15479,7 +15488,7 @@ static void f_setpos(typval_T *argvars, typval_T *rettv)
*/ */
static void f_setqflist(typval_T *argvars, typval_T *rettv) static void f_setqflist(typval_T *argvars, typval_T *rettv)
{ {
set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv); set_qf_ll_list(NULL, argvars, rettv);
} }
/* /*