Problem: Memory leak when compiling inline function.
Solution: Free the prefetched line.
evalarg->eval_tofree = NULL;
}
- vim_free(evalarg->eval_tofree_lambda);
- evalarg->eval_tofree_lambda = NULL;
+ VIM_CLEAR(evalarg->eval_tofree_cmdline);
+ VIM_CLEAR(evalarg->eval_tofree_lambda);
}
}
// Passed to an eval() function to enable evaluation.
EXTERN evalarg_T EVALARG_EVALUATE
# ifdef DO_INIT
- = {EVAL_EVALUATE, 0, NULL, NULL, NULL, {0, 0, 0, 0, NULL}, NULL, NULL}
+ = {EVAL_EVALUATE, 0, NULL, NULL, NULL, {0, 0, 0, 0, NULL},
+ NULL, NULL, NULL}
# endif
;
#endif
// pointer to the last line obtained with getsourceline()
char_u *eval_tofree;
+ // pointer to the last line of an inline function
+ char_u *eval_tofree_cmdline;
+
// pointer to the lines concatenated for a lambda.
char_u *eval_tofree_lambda;
} evalarg_T;
ga_init2(&newlines, (int)sizeof(char_u *), 10);
if (get_function_body(&eap, &newlines, NULL, &line_to_free) == FAIL)
+ {
+ vim_free(cmdline);
goto erret;
+ }
if (cmdline != NULL)
{
// Something comes after the "}".
*arg = eap.nextcmd;
- if (evalarg->eval_cctx == NULL)
- {
- // Need to keep the line and free it/ later.
- vim_free(evalarg->eval_tofree_lambda);
- evalarg->eval_tofree_lambda = cmdline;
- }
+
+ // "arg" points into cmdline, need to keep the line and free it later.
+ vim_free(evalarg->eval_tofree_cmdline);
+ evalarg->eval_tofree_cmdline = cmdline;
}
else
*arg = (char_u *)"";
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 2636,
/**/
2635,
/**/
// Compile the function into instructions.
compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
+ // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
+ // points into it. Point to the original line to avoid a dangling pointer.
+ if (evalarg.eval_tofree_cmdline != NULL)
+ {
+ size_t off = *arg - evalarg.eval_tofree_cmdline;
+
+ *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
+ + off;
+ }
+
clear_evalarg(&evalarg, NULL);
if (ufunc->uf_def_status == UF_COMPILED)