Problem: Vim9: leaking memory when using continuation line.
Solution: Keep a pointer to the continuation line in evalarg_T. Centralize
checking for a next command.
++textwinlock;
vim_free(result);
- result = eval_to_string(bexpr, NULL, TRUE);
+ result = eval_to_string(bexpr, TRUE);
// Remove one trailing newline, it is added when the result was a
// list and it's hardly ever useful. If the user really wants a
tv.vval.v_number = wp->w_id;
set_var((char_u *)"g:statusline_winid", &tv, FALSE);
- usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
+ usefmt = eval_to_string_safe(fmt + 2, use_sandbox);
if (usefmt == NULL)
usefmt = fmt;
if (curwin != save_curwin)
VIsual_active = FALSE;
- str = eval_to_string_safe(p, &t, use_sandbox);
+ str = eval_to_string_safe(p, use_sandbox);
curwin = save_curwin;
curbuf = save_curbuf;
// to be typed. Do generate errors so that try/catch works.
++emsg_silent;
- res = eval_to_string(expr, NULL, TRUE);
+ res = eval_to_string(expr, TRUE);
debug_break_level = save_dbl;
redir_off = save_ro;
eval_to_bool(
char_u *arg,
int *error,
- char_u **nextcmd,
+ exarg_T *eap,
int skip) // only parse, don't execute
{
typval_T tv;
if (skip)
++emsg_skip;
- if (eval0(arg, &tv, nextcmd, skip ? NULL : &EVALARG_EVALUATE) == FAIL)
+ if (eval0(arg, &tv, eap, skip ? NULL : &EVALARG_EVALUATE) == FAIL)
*error = TRUE;
else
{
char_u *
eval_to_string_skip(
char_u *arg,
- char_u **nextcmd,
+ exarg_T *eap,
int skip) // only parse, don't execute
{
typval_T tv;
if (skip)
++emsg_skip;
- if (eval0(arg, &tv, nextcmd, skip ? NULL : &EVALARG_EVALUATE)
+ if (eval0(arg, &tv, eap, skip ? NULL : &EVALARG_EVALUATE)
== FAIL || skip)
retval = NULL;
else
char_u *
eval_to_string(
char_u *arg,
- char_u **nextcmd,
int convert)
{
typval_T tv;
char_u numbuf[NUMBUFLEN];
#endif
- if (eval0(arg, &tv, nextcmd, &EVALARG_EVALUATE) == FAIL)
+ if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
retval = NULL;
else
{
char_u *
eval_to_string_safe(
char_u *arg,
- char_u **nextcmd,
int use_sandbox)
{
char_u *retval;
if (use_sandbox)
++sandbox;
++textwinlock;
- retval = eval_to_string(arg, nextcmd, FALSE);
+ retval = eval_to_string(arg, FALSE);
if (use_sandbox)
--sandbox;
--textwinlock;
* Returns NULL when there is an error.
*/
typval_T *
-eval_expr(char_u *arg, char_u **nextcmd)
+eval_expr(char_u *arg, exarg_T *eap)
{
typval_T *tv;
tv = ALLOC_ONE(typval_T);
- if (tv != NULL && eval0(arg, tv, nextcmd, &EVALARG_EVALUATE) == FAIL)
+ if (tv != NULL && eval0(arg, tv, eap, &EVALARG_EVALUATE) == FAIL)
VIM_CLEAR(tv);
return tv;
eval_for_line(
char_u *arg,
int *errp,
- char_u **nextcmdp,
+ exarg_T *eap,
int skip)
{
forinfo_T *fi;
if (skip)
++emsg_skip;
- if (eval0(skipwhite(expr + 2), &tv, nextcmdp, &evalarg) == OK)
+ if (eval0(skipwhite(expr + 2), &tv, eap, &evalarg) == OK)
{
*errp = FALSE;
if (!skip)
return arg;
}
+/*
+ * To be called when eval_next_non_blank() sets "getnext" to TRUE.
+ */
+ static char_u *
+eval_next_line(evalarg_T *evalarg)
+{
+ vim_free(evalarg->eval_tofree);
+ evalarg->eval_tofree = getsourceline(0, evalarg->eval_cookie, 0, TRUE);
+ return skipwhite(evalarg->eval_tofree);
+}
+
/*
* The "evaluate" argument: When FALSE, the argument is only parsed but not
* executed. The function may return OK, but the rettv will be of type
eval0(
char_u *arg,
typval_T *rettv,
- char_u **nextcmd,
+ exarg_T *eap,
evalarg_T *evalarg)
{
int ret;
int called_emsg_before = called_emsg;
int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
+ if (evalarg != NULL)
+ evalarg->eval_tofree = NULL;
p = skipwhite(arg);
ret = eval1(&p, rettv, evalarg);
+
if (ret == FAIL || !ends_excmd2(arg, p))
{
if (ret != FAIL)
semsg(_(e_invexpr2), arg);
ret = FAIL;
}
- if (nextcmd != NULL)
- *nextcmd = check_nextcmd(p);
+
+ if (eap != NULL)
+ eap->nextcmd = check_nextcmd(p);
+
+ if (evalarg != NULL)
+ {
+ if (eap != NULL)
+ {
+ if (evalarg->eval_tofree != NULL)
+ {
+ // We may need to keep the original command line, e.g. for
+ // ":let" it has the variable names. But we may also need the
+ // new one, "nextcmd" points into it. Keep both.
+ vim_free(eap->cmdline_tofree);
+ eap->cmdline_tofree = *eap->cmdlinep;
+ *eap->cmdlinep = evalarg->eval_tofree;
+ }
+ }
+ else
+ vim_free(evalarg->eval_tofree);
+ }
return ret;
}
if (op != '+' && op != '-' && !concat)
break;
if (getnext)
- *arg = skipwhite(getsourceline(0, evalarg->eval_cookie, 0, TRUE));
+ *arg = eval_next_line(evalarg);
if ((op != '+' || (rettv->v_type != VAR_LIST
&& rettv->v_type != VAR_BLOB))
if (op != '*' && op != '/' && op != '%')
break;
if (getnext)
- *arg = skipwhite(getsourceline(0, evalarg->eval_cookie, 0, TRUE));
+ *arg = eval_next_line(evalarg);
if (evaluate)
{
char_u c1;
char_u *retval = NULL;
char_u *temp_result;
- char_u *nextcmd = NULL;
if (expr_end == NULL || in_end == NULL)
return NULL;
c1 = *in_end;
*in_end = NUL;
- temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
- if (temp_result != NULL && nextcmd == NULL)
+ temp_result = eval_to_string(expr_start + 1, FALSE);
+ if (temp_result != NULL)
{
retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
+ (in_end - expr_end) + 1);
evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
evalarg.eval_cookie = eap->getline == getsourceline
? eap->cookie : NULL;
- i = eval0(expr, &rettv, &eap->nextcmd, &evalarg);
+ i = eval0(expr, &rettv, eap, &evalarg);
if (eap->skip)
--emsg_skip;
}
#ifdef FEAT_EVAL
--ex_nesting_level;
+ vim_free(ea.cmdline_tofree);
#endif
return ea.nextcmd;
if (expr != NULL)
{
++emsg_off;
- p = eval_to_string(expr, NULL, FALSE);
+ p = eval_to_string(expr, FALSE);
--emsg_off;
vim_free(expr);
}
evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
evalarg.eval_cookie = eap->getline == getsourceline ? eap->cookie : NULL;
- if (eval0(eap->arg, &tv, &eap->nextcmd, &evalarg) == OK)
+ if (eval0(eap->arg, &tv, eap, &evalarg) == OK)
clear_tv(&tv);
}
skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
&& !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
- result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
+ result = eval_to_bool(eap->arg, &error, eap, skip);
if (!skip && !error)
{
if (eap->cmdidx == CMD_elseif)
{
- result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
+ result = eval_to_bool(eap->arg, &error, eap, skip);
// When throwing error exceptions, we want to throw always the first
// of several errors in a row. This is what actually happens when
/*
* ":while bool-expr"
*/
- result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
+ result = eval_to_bool(eap->arg, &error, eap, skip);
}
else
{
else
{
// Evaluate the argument and get the info in a structure.
- fi = eval_for_line(eap->arg, &error, &eap->nextcmd, skip);
+ fi = eval_for_line(eap->arg, &error, eap, skip);
cstack->cs_forinfo[cstack->cs_idx] = fi;
}
char_u *value;
if (*arg != NUL && *arg != '|' && *arg != '\n')
- value = eval_to_string_skip(arg, &eap->nextcmd, eap->skip);
+ value = eval_to_string_skip(arg, eap, eap->skip);
else
{
emsg(_(e_argreq));
#ifdef FEAT_EVAL
if (*cmd == '=') // `={expr}`: Expand expression
- buffer = eval_to_string(cmd + 1, &p, TRUE);
+ buffer = eval_to_string(cmd + 1, TRUE);
else
#endif
buffer = get_cmd_output(cmd, NULL,
char_u *res;
set_vim_var_string(VV_FNAME, ptr, len);
- res = eval_to_string_safe(curbuf->b_p_inex, NULL,
+ res = eval_to_string_safe(curbuf->b_p_inex,
was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
set_vim_var_string(VV_FNAME, NULL, 0);
return res;
curbuf = wp->w_buffer;
++emsg_silent; // handle exceptions, but don't display errors
- text = eval_to_string_safe(wp->w_p_fdt, NULL,
+ text = eval_to_string_safe(wp->w_p_fdt,
was_set_insecurely((char_u *)"foldtext", OPT_LOCAL));
--emsg_silent;
EXTERN listitem_T range_list_item;
// Passed to an eval() function to enable evaluation.
-EXTERN evalarg_T EVALARG_EVALUATE INIT2(EVAL_EVALUATE, NULL);
+EXTERN evalarg_T EVALARG_EVALUATE INIT3(EVAL_EVALUATE, NULL, NULL);
#endif
#ifdef MSWIN
/* Evaluate the expression */
++emsg_skip;
- str = (char *)eval_to_string((char_u *)buffer, NULL, TRUE);
+ str = (char *)eval_to_string((char_u *)buffer, TRUE);
--emsg_skip;
vim_free(buffer);
if (str == NULL)
char_u *
eval_to_string(
char_u *arg UNUSED,
- char_u **nextcmd UNUSED,
int dolist UNUSED)
{
return NULL;
PREINIT:
char_u *value;
PPCODE:
- value = eval_to_string((char_u *)str, (char_u **)0, TRUE);
+ value = eval_to_string((char_u *)str, TRUE);
if (value == NULL)
{
XPUSHs(sv_2mortal(newSViv(0)));
#ifdef FEAT_EVAL
expr = Tcl_GetStringFromObj(objv[objn], NULL);
- str = (char *)eval_to_string((char_u *)expr, NULL, TRUE);
+ str = (char *)eval_to_string((char_u *)expr, TRUE);
if (str == NULL)
Tcl_SetResult(interp, _("invalid expression"), TCL_STATIC);
else
save_cursor = curwin->w_cursor;
save_msg_col = msg_col;
save_msg_row = msg_row;
- p = eval_to_string(expr, NULL, FALSE);
+ p = eval_to_string(expr, FALSE);
--textwinlock;
--ex_normal_lock;
curwin->w_cursor = save_cursor;
varnumber_T num_modulus(varnumber_T n1, varnumber_T n2);
void eval_init(void);
void eval_clear(void);
-int eval_to_bool(char_u *arg, int *error, char_u **nextcmd, int skip);
+int eval_to_bool(char_u *arg, int *error, exarg_T *eap, int skip);
int eval_expr_valid_arg(typval_T *tv);
int eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv);
int eval_expr_to_bool(typval_T *expr, int *error);
-char_u *eval_to_string_skip(char_u *arg, char_u **nextcmd, int skip);
+char_u *eval_to_string_skip(char_u *arg, exarg_T *eap, int skip);
int skip_expr(char_u **pp);
-char_u *eval_to_string(char_u *arg, char_u **nextcmd, int convert);
-char_u *eval_to_string_safe(char_u *arg, char_u **nextcmd, int use_sandbox);
+char_u *eval_to_string(char_u *arg, int convert);
+char_u *eval_to_string_safe(char_u *arg, int use_sandbox);
varnumber_T eval_to_number(char_u *expr);
-typval_T *eval_expr(char_u *arg, char_u **nextcmd);
+typval_T *eval_expr(char_u *arg, exarg_T *eap);
int call_vim_function(char_u *func, int argc, typval_T *argv, typval_T *rettv);
varnumber_T call_func_retnr(char_u *func, int argc, typval_T *argv);
void *call_func_retstr(char_u *func, int argc, typval_T *argv);
char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
void clear_lval(lval_T *lp);
void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int flags, char_u *op);
-void *eval_for_line(char_u *arg, int *errp, char_u **nextcmdp, int skip);
+void *eval_for_line(char_u *arg, int *errp, exarg_T *eap, int skip);
int next_for_item(void *fi_void, char_u *arg);
void free_for_info(void *fi_void);
void set_context_for_expression(expand_T *xp, char_u *arg, cmdidx_T cmdidx);
int pattern_match(char_u *pat, char_u *text, int ic);
-int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, evalarg_T *evalarg);
-int eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg_in);
+int eval0(char_u *arg, typval_T *rettv, exarg_T *eap, evalarg_T *evalarg);
+int eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
void eval_addblob(typval_T *tv1, typval_T *tv2);
int eval_addlist(typval_T *tv1, typval_T *tv2);
char_u *partial_name(partial_T *pt);
// Evaluate the expression. When the result is a string or a list we can
// use it to fill the errorlist.
- tv = eval_expr(eap->arg, &eap->nextcmd);
+ tv = eval_expr(eap->arg, eap);
if (tv != NULL)
{
if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL)
clear_tv(&rettv);
}
else
- eval_result = eval_to_string(source + 2, NULL, TRUE);
+ eval_result = eval_to_string(source + 2, TRUE);
if (eval_result != NULL)
{
return expr_copy;
++nested;
- rv = eval_to_string(expr_copy, NULL, TRUE);
+ rv = eval_to_string(expr_copy, TRUE);
--nested;
vim_free(expr_copy);
return rv;
curwin = wp;
STRCPY(buf, "b:keymap_name"); // must be writable
++emsg_skip;
- s = p = eval_to_string(buf, NULL, FALSE);
+ s = p = eval_to_string(buf, FALSE);
--emsg_skip;
curbuf = old_curbuf;
curwin = old_curwin;
// copied from exarg_T when "getline" is "getsourceline". Can be NULL.
void *eval_cookie; // argument for getline()
+
+ // pointer to the line obtained with getsourceline()
+ char_u *eval_tofree;
} evalarg_T;
// Flags for expression evaluation.
eap->nextcmd = NULL;
if ((*arg != NUL && *arg != '|' && *arg != '\n')
- && eval0(arg, &rettv, &eap->nextcmd, &evalarg) != FAIL)
+ && eval0(arg, &rettv, eap, &evalarg) != FAIL)
{
if (!eap->skip)
returning = do_return(eap, FALSE, TRUE, &rettv);
// instead to skip to any following command, e.g. for:
// :if 0 | call dict.foo().bar() | endif
++emsg_skip;
- if (eval0(eap->arg, &rettv, &eap->nextcmd, NULL) != FAIL)
+ if (eval0(eap->arg, &rettv, eap, NULL) != FAIL)
clear_tv(&rettv);
--emsg_skip;
return;
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 1049,
/**/
1048,
/**/