patch 8.2.1049: Vim9: leaking memory when using continuation line

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.
diff --git a/src/beval.c b/src/beval.c
index b7d9226..dd7bc3c 100644
--- a/src/beval.c
+++ b/src/beval.c
@@ -285,7 +285,7 @@
 	    ++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
diff --git a/src/buffer.c b/src/buffer.c
index f928412..40ca25d 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -4094,7 +4094,7 @@
 	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;
 
@@ -4434,7 +4434,7 @@
 	    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;
diff --git a/src/clientserver.c b/src/clientserver.c
index cf35c81..fbbeb6a 100644
--- a/src/clientserver.c
+++ b/src/clientserver.c
@@ -86,7 +86,7 @@
     // 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;
diff --git a/src/eval.c b/src/eval.c
index ffb69de..79944c8 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -161,7 +161,7 @@
 eval_to_bool(
     char_u	*arg,
     int		*error,
-    char_u	**nextcmd,
+    exarg_T	*eap,
     int		skip)	    // only parse, don't execute
 {
     typval_T	tv;
@@ -169,7 +169,7 @@
 
     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
     {
@@ -317,7 +317,7 @@
     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;
@@ -325,7 +325,7 @@
 
     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
@@ -361,7 +361,6 @@
     char_u *
 eval_to_string(
     char_u	*arg,
-    char_u	**nextcmd,
     int		convert)
 {
     typval_T	tv;
@@ -371,7 +370,7 @@
     char_u	numbuf[NUMBUFLEN];
 #endif
 
-    if (eval0(arg, &tv, nextcmd, &EVALARG_EVALUATE) == FAIL)
+    if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
 	retval = NULL;
     else
     {
@@ -409,7 +408,6 @@
     char_u *
 eval_to_string_safe(
     char_u	*arg,
-    char_u	**nextcmd,
     int		use_sandbox)
 {
     char_u	*retval;
@@ -419,7 +417,7 @@
     if (use_sandbox)
 	++sandbox;
     ++textwinlock;
-    retval = eval_to_string(arg, nextcmd, FALSE);
+    retval = eval_to_string(arg, FALSE);
     if (use_sandbox)
 	--sandbox;
     --textwinlock;
@@ -459,12 +457,12 @@
  * 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;
@@ -1418,7 +1416,7 @@
 eval_for_line(
     char_u	*arg,
     int		*errp,
-    char_u	**nextcmdp,
+    exarg_T	*eap,
     int		skip)
 {
     forinfo_T	*fi;
@@ -1448,7 +1446,7 @@
 
     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)
@@ -1796,6 +1794,17 @@
 }
 
 /*
+ * 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
  * VAR_UNKNOWN.  The function still returns FAIL for a syntax error.
@@ -1813,7 +1822,7 @@
 eval0(
     char_u	*arg,
     typval_T	*rettv,
-    char_u	**nextcmd,
+    exarg_T	*eap,
     evalarg_T	*evalarg)
 {
     int		ret;
@@ -1822,8 +1831,11 @@
     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)
@@ -1841,8 +1853,27 @@
 	    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;
 }
@@ -2305,7 +2336,7 @@
 	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))
@@ -2497,7 +2528,7 @@
 	if (op != '*' && op != '/' && op != '%')
 	    break;
 	if (getnext)
-	    *arg = skipwhite(getsourceline(0, evalarg->eval_cookie, 0, TRUE));
+	    *arg = eval_next_line(evalarg);
 
 	if (evaluate)
 	{
@@ -4734,7 +4765,6 @@
     char_u	c1;
     char_u	*retval = NULL;
     char_u	*temp_result;
-    char_u	*nextcmd = NULL;
 
     if (expr_end == NULL || in_end == NULL)
 	return NULL;
@@ -4743,8 +4773,8 @@
     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);
diff --git a/src/evalvars.c b/src/evalvars.c
index 35c9dcb..e64b44e 100644
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -800,7 +800,7 @@
 	    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;
 	}
diff --git a/src/ex_docmd.c b/src/ex_docmd.c
index 4755a01..6c887fa 100644
--- a/src/ex_docmd.c
+++ b/src/ex_docmd.c
@@ -2609,6 +2609,7 @@
 
 #ifdef FEAT_EVAL
     --ex_nesting_level;
+    vim_free(ea.cmdline_tofree);
 #endif
 
     return ea.nextcmd;
@@ -4912,7 +4913,7 @@
 	if (expr != NULL)
 	{
 	    ++emsg_off;
-	    p = eval_to_string(expr, NULL, FALSE);
+	    p = eval_to_string(expr, FALSE);
 	    --emsg_off;
 	    vim_free(expr);
 	}
diff --git a/src/ex_eval.c b/src/ex_eval.c
index cb32bd0..8b8a256 100644
--- a/src/ex_eval.c
+++ b/src/ex_eval.c
@@ -900,7 +900,7 @@
     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);
 }
 
@@ -929,7 +929,7 @@
 	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)
 	{
@@ -1041,7 +1041,7 @@
 
     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
@@ -1103,7 +1103,7 @@
 	    /*
 	     * ":while bool-expr"
 	     */
-	    result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip);
+	    result = eval_to_bool(eap->arg, &error, eap, skip);
 	}
 	else
 	{
@@ -1122,7 +1122,7 @@
 	    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;
 	    }
 
@@ -1322,7 +1322,7 @@
     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));
diff --git a/src/filepath.c b/src/filepath.c
index 1fe757e..6644e99 100644
--- a/src/filepath.c
+++ b/src/filepath.c
@@ -3083,7 +3083,7 @@
 
 #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,
diff --git a/src/findfile.c b/src/findfile.c
index b153ed0..190fc69 100644
--- a/src/findfile.c
+++ b/src/findfile.c
@@ -2079,7 +2079,7 @@
     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;
diff --git a/src/fold.c b/src/fold.c
index d91203c..043037f 100644
--- a/src/fold.c
+++ b/src/fold.c
@@ -1928,7 +1928,7 @@
 	    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;
 
diff --git a/src/globals.h b/src/globals.h
index 8601e2b..c7f9794 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -1882,7 +1882,7 @@
 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
diff --git a/src/if_ole.cpp b/src/if_ole.cpp
index 34ce232..e415a19 100644
--- a/src/if_ole.cpp
+++ b/src/if_ole.cpp
@@ -388,7 +388,7 @@
 
     /* 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)
diff --git a/src/if_perl.xs b/src/if_perl.xs
index bf269cb..cad571c 100644
--- a/src/if_perl.xs
+++ b/src/if_perl.xs
@@ -832,7 +832,6 @@
     char_u *
 eval_to_string(
     char_u	*arg UNUSED,
-    char_u	**nextcmd UNUSED,
     int		dolist UNUSED)
 {
     return NULL;
@@ -1562,7 +1561,7 @@
     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)));
diff --git a/src/if_tcl.c b/src/if_tcl.c
index c274b26..2775221 100644
--- a/src/if_tcl.c
+++ b/src/if_tcl.c
@@ -1373,7 +1373,7 @@
 
 #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
diff --git a/src/map.c b/src/map.c
index 4f0c870..26fce68 100644
--- a/src/map.c
+++ b/src/map.c
@@ -1614,7 +1614,7 @@
     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;
diff --git a/src/proto/eval.pro b/src/proto/eval.pro
index 4fb04eb..87e4478 100644
--- a/src/proto/eval.pro
+++ b/src/proto/eval.pro
@@ -3,16 +3,16 @@
 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);
@@ -21,13 +21,13 @@
 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);
diff --git a/src/quickfix.c b/src/quickfix.c
index ba54fab..3bd0f75 100644
--- a/src/quickfix.c
+++ b/src/quickfix.c
@@ -7680,7 +7680,7 @@
 
     // 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)
diff --git a/src/regexp.c b/src/regexp.c
index de0b0fa..229f6ef 100644
--- a/src/regexp.c
+++ b/src/regexp.c
@@ -2066,7 +2066,7 @@
 		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)
 	    {
diff --git a/src/register.c b/src/register.c
index 49f7a7c..66dd0cc 100644
--- a/src/register.c
+++ b/src/register.c
@@ -136,7 +136,7 @@
 	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;
diff --git a/src/screen.c b/src/screen.c
index ea7aaa6..01d6257 100644
--- a/src/screen.c
+++ b/src/screen.c
@@ -1148,7 +1148,7 @@
 	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;
diff --git a/src/structs.h b/src/structs.h
index 0ac4864..3950887 100644
--- a/src/structs.h
+++ b/src/structs.h
@@ -1753,6 +1753,9 @@
 
     // 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.
diff --git a/src/userfunc.c b/src/userfunc.c
index a27c68d..c6a8a8c 100644
--- a/src/userfunc.c
+++ b/src/userfunc.c
@@ -3716,7 +3716,7 @@
 
     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);
@@ -3773,7 +3773,7 @@
 	// 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;
diff --git a/src/version.c b/src/version.c
index 4e2c553..d39c1b0 100644
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1049,
+/**/
     1048,
 /**/
     1047,