patch 8.2.1491: Vim9: crash when compiling heredoc lines start with comment

Problem:    Vim9: crash when compiling heredoc lines start with comment.
Solution:   Skip over NULL pointers. Do not remove comment and empty lines
            when fetching function lines. (closes #6743)
This commit is contained in:
Bram Moolenaar
2020-08-20 15:02:42 +02:00
parent 93ad14710b
commit 66250c932e
16 changed files with 81 additions and 45 deletions

View File

@@ -2310,7 +2310,11 @@ auto_next_pat(
* Returns allocated string, or NULL for end of autocommands. * Returns allocated string, or NULL for end of autocommands.
*/ */
char_u * char_u *
getnextac(int c UNUSED, void *cookie, int indent UNUSED, int do_concat UNUSED) getnextac(
int c UNUSED,
void *cookie,
int indent UNUSED,
getline_opt_T options UNUSED)
{ {
AutoPatCmd *acp = (AutoPatCmd *)cookie; AutoPatCmd *acp = (AutoPatCmd *)cookie;
char_u *retval; char_u *retval;

View File

@@ -2198,13 +2198,12 @@ execute_redir_str(char_u *value, int value_len)
* Called by do_cmdline() to get the next line. * Called by do_cmdline() to get the next line.
* Returns allocated string, or NULL for end of function. * Returns allocated string, or NULL for end of function.
*/ */
static char_u * static char_u *
get_list_line( get_list_line(
int c UNUSED, int c UNUSED,
void *cookie, void *cookie,
int indent UNUSED, int indent UNUSED,
int do_concat UNUSED) getline_opt_T options UNUSED)
{ {
listitem_T **p = (listitem_T **)cookie; listitem_T **p = (listitem_T **)cookie;
listitem_T *item = *p; listitem_T *item = *p;

View File

@@ -1867,7 +1867,7 @@ struct exarg
int bad_char; // BAD_KEEP, BAD_DROP or replacement byte int bad_char; // BAD_KEEP, BAD_DROP or replacement byte
int useridx; // user command index int useridx; // user command index
char *errmsg; // returned error message char *errmsg; // returned error message
char_u *(*getline)(int, void *, int, int); char_u *(*getline)(int, void *, int, getline_opt_T);
void *cookie; // argument for getline() void *cookie; // argument for getline()
#ifdef FEAT_EVAL #ifdef FEAT_EVAL
cstack_T *cstack; // condition stack for ":if" etc. cstack_T *cstack; // condition stack for ":if" etc.

View File

@@ -612,7 +612,7 @@ do_cmdline_cmd(char_u *cmd)
int int
do_cmdline( do_cmdline(
char_u *cmdline, char_u *cmdline,
char_u *(*fgetline)(int, void *, int, int), char_u *(*fgetline)(int, void *, int, getline_opt_T),
void *cookie, // argument for fgetline() void *cookie, // argument for fgetline()
int flags) int flags)
{ {
@@ -638,7 +638,7 @@ do_cmdline(
msglist_T *private_msg_list; msglist_T *private_msg_list;
// "fgetline" and "cookie" passed to do_one_cmd() // "fgetline" and "cookie" passed to do_one_cmd()
char_u *(*cmd_getline)(int, void *, int, int); char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
void *cmd_cookie; void *cmd_cookie;
struct loop_cookie cmd_loop_cookie; struct loop_cookie cmd_loop_cookie;
void *real_cookie; void *real_cookie;
@@ -1482,9 +1482,9 @@ free_cmdlines(garray_T *gap)
*/ */
int int
getline_equal( getline_equal(
char_u *(*fgetline)(int, void *, int, int), char_u *(*fgetline)(int, void *, int, getline_opt_T),
void *cookie UNUSED, // argument for fgetline() void *cookie UNUSED, // argument for fgetline()
char_u *(*func)(int, void *, int, int)) char_u *(*func)(int, void *, int, getline_opt_T))
{ {
#ifdef FEAT_EVAL #ifdef FEAT_EVAL
char_u *(*gp)(int, void *, int, int); char_u *(*gp)(int, void *, int, int);
@@ -1512,7 +1512,7 @@ getline_equal(
*/ */
void * void *
getline_cookie( getline_cookie(
char_u *(*fgetline)(int, void *, int, int) UNUSED, char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
void *cookie) // argument for fgetline() void *cookie) // argument for fgetline()
{ {
#ifdef FEAT_EVAL #ifdef FEAT_EVAL
@@ -1541,7 +1541,7 @@ getline_cookie(
*/ */
char_u * char_u *
getline_peek( getline_peek(
char_u *(*fgetline)(int, void *, int, int) UNUSED, char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
void *cookie) // argument for fgetline() void *cookie) // argument for fgetline()
{ {
char_u *(*gp)(int, void *, int, int); char_u *(*gp)(int, void *, int, int);

View File

@@ -2705,12 +2705,12 @@ getexline(
int c, // normally ':', NUL for ":append" int c, // normally ':', NUL for ":append"
void *cookie UNUSED, void *cookie UNUSED,
int indent, // indent for inside conditionals int indent, // indent for inside conditionals
int do_concat) getline_opt_T options)
{ {
// When executing a register, remove ':' that's in front of each line. // When executing a register, remove ':' that's in front of each line.
if (exec_from_reg && vpeekc() == ':') if (exec_from_reg && vpeekc() == ':')
(void)vgetc(); (void)vgetc();
return getcmdline(c, 1L, indent, do_concat); return getcmdline(c, 1L, indent, options);
} }
/* /*
@@ -2725,7 +2725,7 @@ getexmodeline(
// :s prompt // :s prompt
void *cookie UNUSED, void *cookie UNUSED,
int indent, // indent for inside conditionals int indent, // indent for inside conditionals
int do_concat UNUSED) getline_opt_T options UNUSED)
{ {
garray_T line_ga; garray_T line_ga;
char_u *pend; char_u *pend;

View File

@@ -28,7 +28,7 @@ int has_completechanged(void);
void block_autocmds(void); void block_autocmds(void);
void unblock_autocmds(void); void unblock_autocmds(void);
int is_autocmd_blocked(void); int is_autocmd_blocked(void);
char_u *getnextac(int c, void *cookie, int indent, int do_concat); char_u *getnextac(int c, void *cookie, int indent, getline_opt_T options);
int has_autocmd(event_T event, char_u *sfname, buf_T *buf); int has_autocmd(event_T event, char_u *sfname, buf_T *buf);
char_u *get_augroup_name(expand_T *xp, int idx); char_u *get_augroup_name(expand_T *xp, int idx);
char_u *set_context_in_autocmd(expand_T *xp, char_u *arg, int doautocmd); char_u *set_context_in_autocmd(expand_T *xp, char_u *arg, int doautocmd);

View File

@@ -1,10 +1,10 @@
/* ex_docmd.c */ /* ex_docmd.c */
void do_exmode(int improved); void do_exmode(int improved);
int do_cmdline_cmd(char_u *cmd); int do_cmdline_cmd(char_u *cmd);
int do_cmdline(char_u *cmdline, char_u *(*fgetline)(int, void *, int, int), void *cookie, int flags); int do_cmdline(char_u *cmdline, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie, int flags);
int getline_equal(char_u *(*fgetline)(int, void *, int, int), void *cookie, char_u *(*func)(int, void *, int, int)); int getline_equal(char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie, char_u *(*func)(int, void *, int, getline_opt_T));
void *getline_cookie(char_u *(*fgetline)(int, void *, int, int), void *cookie); void *getline_cookie(char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
char_u *getline_peek(char_u *(*fgetline)(int, void *, int, int), void *cookie); char_u *getline_peek(char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
char *ex_errmsg(char *msg, char_u *arg); char *ex_errmsg(char *msg, char_u *arg);
int parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only); int parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only);
void undo_cmdmod(exarg_T *eap, int save_msg_scroll); void undo_cmdmod(exarg_T *eap, int save_msg_scroll);

View File

@@ -9,8 +9,8 @@ char *get_text_locked_msg(void);
int text_locked(void); int text_locked(void);
int curbuf_locked(void); int curbuf_locked(void);
int allbuf_locked(void); int allbuf_locked(void);
char_u *getexline(int c, void *cookie, int indent, int do_concat); char_u *getexline(int c, void *cookie, int indent, getline_opt_T options);
char_u *getexmodeline(int promptc, void *cookie, int indent, int do_concat); char_u *getexmodeline(int promptc, void *cookie, int indent, getline_opt_T options);
int cmdline_overstrike(void); int cmdline_overstrike(void);
int cmdline_at_end(void); int cmdline_at_end(void);
colnr_T cmdline_getvcol_cursor(void); colnr_T cmdline_getvcol_cursor(void);

View File

@@ -29,13 +29,13 @@ void scriptnames_slash_adjust(void);
char_u *get_scriptname(scid_T id); char_u *get_scriptname(scid_T id);
void free_scriptnames(void); void free_scriptnames(void);
void free_autoload_scriptnames(void); void free_autoload_scriptnames(void);
linenr_T get_sourced_lnum(char_u *(*fgetline)(int, void *, int, int), void *cookie); linenr_T get_sourced_lnum(char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
char_u *getsourceline(int c, void *cookie, int indent, int do_concat); char_u *getsourceline(int c, void *cookie, int indent, getline_opt_T options);
void ex_scriptencoding(exarg_T *eap); void ex_scriptencoding(exarg_T *eap);
void ex_scriptversion(exarg_T *eap); void ex_scriptversion(exarg_T *eap);
void ex_finish(exarg_T *eap); void ex_finish(exarg_T *eap);
void do_finish(exarg_T *eap, int reanimate); void do_finish(exarg_T *eap, int reanimate);
int source_finished(char_u *(*fgetline)(int, void *, int, int), void *cookie); int source_finished(char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
char_u *autoload_name(char_u *name); char_u *autoload_name(char_u *name);
int script_autoload(char_u *name, int reload); int script_autoload(char_u *name, int reload);
/* vim: set ft=c : */ /* vim: set ft=c : */

View File

@@ -46,7 +46,7 @@ void ex_call(exarg_T *eap);
int do_return(exarg_T *eap, int reanimate, int is_cmd, void *rettv); int do_return(exarg_T *eap, int reanimate, int is_cmd, void *rettv);
void discard_pending_return(void *rettv); void discard_pending_return(void *rettv);
char_u *get_return_cmd(void *rettv); char_u *get_return_cmd(void *rettv);
char_u *get_func_line(int c, void *cookie, int indent, int do_concat); char_u *get_func_line(int c, void *cookie, int indent, getline_opt_T options);
int func_has_ended(void *cookie); int func_has_ended(void *cookie);
int func_has_abort(void *cookie); int func_has_abort(void *cookie);
dict_T *make_partial(dict_T *selfdict_in, typval_T *rettv); dict_T *make_partial(dict_T *selfdict_in, typval_T *rettv);

View File

@@ -1604,7 +1604,9 @@ free_autoload_scriptnames(void)
#endif #endif
linenr_T linenr_T
get_sourced_lnum(char_u *(*fgetline)(int, void *, int, int), void *cookie) get_sourced_lnum(
char_u *(*fgetline)(int, void *, int, getline_opt_T),
void *cookie)
{ {
return fgetline == getsourceline return fgetline == getsourceline
? ((struct source_cookie *)cookie)->sourcing_lnum ? ((struct source_cookie *)cookie)->sourcing_lnum
@@ -1724,7 +1726,11 @@ get_one_sourceline(struct source_cookie *sp)
* Return NULL for end-of-file or some error. * Return NULL for end-of-file or some error.
*/ */
char_u * char_u *
getsourceline(int c UNUSED, void *cookie, int indent UNUSED, int do_concat) getsourceline(
int c UNUSED,
void *cookie,
int indent UNUSED,
getline_opt_T options)
{ {
struct source_cookie *sp = (struct source_cookie *)cookie; struct source_cookie *sp = (struct source_cookie *)cookie;
char_u *line; char_u *line;
@@ -1765,7 +1771,8 @@ getsourceline(int c UNUSED, void *cookie, int indent UNUSED, int do_concat)
// Only concatenate lines starting with a \ when 'cpoptions' doesn't // Only concatenate lines starting with a \ when 'cpoptions' doesn't
// contain the 'C' flag. // contain the 'C' flag.
if (line != NULL && do_concat && vim_strchr(p_cpo, CPO_CONCAT) == NULL) if (line != NULL && options != GETLINE_NONE
&& vim_strchr(p_cpo, CPO_CONCAT) == NULL)
{ {
// compensate for the one line read-ahead // compensate for the one line read-ahead
--sp->sourcing_lnum; --sp->sourcing_lnum;
@@ -1781,6 +1788,7 @@ getsourceline(int c UNUSED, void *cookie, int indent UNUSED, int do_concat)
|| (p[0] == '"' && p[1] == '\\' && p[2] == ' ') || (p[0] == '"' && p[1] == '\\' && p[2] == ' ')
#ifdef FEAT_EVAL #ifdef FEAT_EVAL
|| (in_vim9script() || (in_vim9script()
&& options == GETLINE_CONCAT_ALL
&& (*p == NUL || vim9_comment_start(p))) && (*p == NUL || vim9_comment_start(p)))
#endif #endif
)) ))
@@ -1814,6 +1822,7 @@ getsourceline(int c UNUSED, void *cookie, int indent UNUSED, int do_concat)
else if (!(p[0] == '"' && p[1] == '\\' && p[2] == ' ') else if (!(p[0] == '"' && p[1] == '\\' && p[2] == ' ')
#ifdef FEAT_EVAL #ifdef FEAT_EVAL
&& !(in_vim9script() && !(in_vim9script()
&& options == GETLINE_CONCAT_ALL
&& (*p == NUL || vim9_comment_start(p))) && (*p == NUL || vim9_comment_start(p)))
#endif #endif
) )
@@ -1968,7 +1977,7 @@ do_finish(exarg_T *eap, int reanimate)
*/ */
int int
source_finished( source_finished(
char_u *(*fgetline)(int, void *, int, int), char_u *(*fgetline)(int, void *, int, getline_opt_T),
void *cookie) void *cookie)
{ {
return (getline_equal(fgetline, cookie, getsourceline) return (getline_equal(fgetline, cookie, getsourceline)

View File

@@ -1761,6 +1761,13 @@ typedef struct
# endif # endif
} scriptitem_T; } scriptitem_T;
// type of getline() last argument
typedef enum {
GETLINE_NONE, // do not concatenate any lines
GETLINE_CONCAT_CONT, // concatenate continuation lines
GETLINE_CONCAT_ALL // concatenate continuation and Vim9 # comment lines
} getline_opt_T;
// Struct passed through eval() functions. // Struct passed through eval() functions.
// See EVALARG_EVALUATE for a fixed value with eval_flags set to EVAL_EVALUATE. // See EVALARG_EVALUATE for a fixed value with eval_flags set to EVAL_EVALUATE.
typedef struct { typedef struct {
@@ -1768,7 +1775,7 @@ typedef struct {
int eval_break_count; // nr of line breaks consumed int eval_break_count; // nr of line breaks consumed
// copied from exarg_T when "getline" is "getsourceline". Can be NULL. // copied from exarg_T when "getline" is "getsourceline". Can be NULL.
char_u *(*eval_getline)(int, void *, int, int); char_u *(*eval_getline)(int, void *, int, getline_opt_T);
void *eval_cookie; // argument for eval_getline() void *eval_cookie; // argument for eval_getline()
// used when compiling a :def function, NULL otherwise // used when compiling a :def function, NULL otherwise

View File

@@ -1141,6 +1141,17 @@ def Test_list_vimscript()
assert_equal(['one', 'two', 'three'], mylist) assert_equal(['one', 'two', 'three'], mylist)
END END
CheckScriptSuccess(lines) CheckScriptSuccess(lines)
# check all lines from heredoc are kept
lines =<< trim END
# comment 1
two
# comment 3
five
# comment 6
END
assert_equal(['# comment 1', 'two', '# comment 3', '', 'five', '# comment 6'], lines)
enddef enddef
if has('channel') if has('channel')

View File

@@ -2651,7 +2651,7 @@ def_function(exarg_T *eap, char_u *name_arg)
static int func_nr = 0; // number for nameless function static int func_nr = 0; // number for nameless function
int paren; int paren;
hashitem_T *hi; hashitem_T *hi;
int do_concat = TRUE; getline_opt_T getline_options = GETLINE_CONCAT_CONT;
linenr_T sourcing_lnum_off; linenr_T sourcing_lnum_off;
linenr_T sourcing_lnum_top; linenr_T sourcing_lnum_top;
int is_heredoc = FALSE; int is_heredoc = FALSE;
@@ -3008,9 +3008,10 @@ def_function(exarg_T *eap, char_u *name_arg)
{ {
vim_free(line_to_free); vim_free(line_to_free);
if (eap->getline == NULL) if (eap->getline == NULL)
theline = getcmdline(':', 0L, indent, do_concat); theline = getcmdline(':', 0L, indent, getline_options);
else else
theline = eap->getline(':', eap->cookie, indent, do_concat); theline = eap->getline(':', eap->cookie, indent,
getline_options);
line_to_free = theline; line_to_free = theline;
} }
if (KeyTyped) if (KeyTyped)
@@ -3053,7 +3054,7 @@ def_function(exarg_T *eap, char_u *name_arg)
{ {
VIM_CLEAR(skip_until); VIM_CLEAR(skip_until);
VIM_CLEAR(heredoc_trimmed); VIM_CLEAR(heredoc_trimmed);
do_concat = TRUE; getline_options = GETLINE_CONCAT_CONT;
is_heredoc = FALSE; is_heredoc = FALSE;
} }
} }
@@ -3178,7 +3179,7 @@ def_function(exarg_T *eap, char_u *name_arg)
skip_until = vim_strsave((char_u *)"."); skip_until = vim_strsave((char_u *)".");
else else
skip_until = vim_strnsave(p, skiptowhite(p) - p); skip_until = vim_strnsave(p, skiptowhite(p) - p);
do_concat = FALSE; getline_options = GETLINE_NONE;
is_heredoc = TRUE; is_heredoc = TRUE;
} }
@@ -3205,7 +3206,7 @@ def_function(exarg_T *eap, char_u *name_arg)
skipwhite(theline) - theline); skipwhite(theline) - theline);
} }
skip_until = vim_strnsave(p, skiptowhite(p) - p); skip_until = vim_strnsave(p, skiptowhite(p) - p);
do_concat = FALSE; getline_options = GETLINE_NONE;
is_heredoc = TRUE; is_heredoc = TRUE;
} }
} }
@@ -4249,7 +4250,7 @@ get_func_line(
int c UNUSED, int c UNUSED,
void *cookie, void *cookie,
int indent UNUSED, int indent UNUSED,
int do_concat UNUSED) getline_opt_T options UNUSED)
{ {
funccall_T *fcp = (funccall_T *)cookie; funccall_T *fcp = (funccall_T *)cookie;
ufunc_T *fp = fcp->func; ufunc_T *fp = fcp->func;

View File

@@ -754,6 +754,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1491,
/**/ /**/
1490, 1490,
/**/ /**/

View File

@@ -4197,18 +4197,21 @@ exarg_getline(
int c UNUSED, int c UNUSED,
void *cookie, void *cookie,
int indent UNUSED, int indent UNUSED,
int do_concat UNUSED) getline_opt_T options UNUSED)
{ {
cctx_T *cctx = (cctx_T *)cookie; cctx_T *cctx = (cctx_T *)cookie;
char_u *p;
if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) for (;;)
{ {
iemsg("Heredoc got to end"); if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
return NULL; return NULL;
}
++cctx->ctx_lnum; ++cctx->ctx_lnum;
return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
[cctx->ctx_lnum]); // Comment lines result in NULL pointers, skip them.
if (p != NULL)
return vim_strsave(p);
}
} }
/* /*