Mercurial > vim
comparison src/option.c @ 15066:40d9218b2b12 v8.1.0544
patch 8.1.0544: setting 'filetype' in a modeline causes an error
commit https://github.com/vim/vim/commit/916a818cea5ba05a5f2117407674461b8bee6832
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Nov 25 02:18:29 2018 +0100
patch 8.1.0544: setting 'filetype' in a modeline causes an error
Problem: Setting 'filetype' in a modeline causes an error (Hirohito
Higashi).
Solution: Don't add the P_INSECURE flag when setting 'filetype' from a
modeline. Also for 'syntax'.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 25 Nov 2018 02:30:10 +0100 |
parents | 3a94f7918980 |
children | 690da0a83419 |
comparison
equal
deleted
inserted
replaced
15065:1916cdaefb0e | 15066:40d9218b2b12 |
---|---|
3282 #endif | 3282 #endif |
3283 | 3283 |
3284 static void set_options_default(int opt_flags); | 3284 static void set_options_default(int opt_flags); |
3285 static void set_string_default_esc(char *name, char_u *val, int escape); | 3285 static void set_string_default_esc(char *name, char_u *val, int escape); |
3286 static char_u *term_bg_default(void); | 3286 static char_u *term_bg_default(void); |
3287 static void did_set_option(int opt_idx, int opt_flags, int new_value); | 3287 static void did_set_option(int opt_idx, int opt_flags, int new_value, int value_checked); |
3288 static char_u *option_expand(int opt_idx, char_u *val); | 3288 static char_u *option_expand(int opt_idx, char_u *val); |
3289 static void didset_options(void); | 3289 static void didset_options(void); |
3290 static void didset_options2(void); | 3290 static void didset_options2(void); |
3291 static void check_string_option(char_u **pp); | 3291 static void check_string_option(char_u **pp); |
3292 #if defined(FEAT_EVAL) || defined(PROTO) | 3292 #if defined(FEAT_EVAL) || defined(PROTO) |
3293 static long_u *insecure_flag(int opt_idx, int opt_flags); | 3293 static long_u *insecure_flag(int opt_idx, int opt_flags); |
3294 #else | 3294 #else |
3295 # define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags) | 3295 # define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags) |
3296 #endif | 3296 #endif |
3297 static void set_string_option_global(int opt_idx, char_u **varp); | 3297 static void set_string_option_global(int opt_idx, char_u **varp); |
3298 static char_u *did_set_string_option(int opt_idx, char_u **varp, int new_value_alloced, char_u *oldval, char_u *errbuf, int opt_flags); | 3298 static char_u *did_set_string_option(int opt_idx, char_u **varp, int new_value_alloced, char_u *oldval, char_u *errbuf, int opt_flags, int *value_checked); |
3299 static char_u *set_chars_option(char_u **varp); | 3299 static char_u *set_chars_option(char_u **varp); |
3300 #ifdef FEAT_CLIPBOARD | 3300 #ifdef FEAT_CLIPBOARD |
3301 static char_u *check_clipboard_option(void); | 3301 static char_u *check_clipboard_option(void); |
3302 #endif | 3302 #endif |
3303 #ifdef FEAT_SPELL | 3303 #ifdef FEAT_SPELL |
4704 errmsg = e_trailing; | 4704 errmsg = e_trailing; |
4705 } | 4705 } |
4706 else | 4706 else |
4707 { | 4707 { |
4708 int value_is_replaced = !prepending && !adding && !removing; | 4708 int value_is_replaced = !prepending && !adding && !removing; |
4709 int value_checked = FALSE; | |
4709 | 4710 |
4710 if (flags & P_BOOL) /* boolean */ | 4711 if (flags & P_BOOL) /* boolean */ |
4711 { | 4712 { |
4712 if (nextchar == '=' || nextchar == ':') | 4713 if (nextchar == '=' || nextchar == ':') |
4713 { | 4714 { |
5234 // Handle side effects, and set the global value for | 5235 // Handle side effects, and set the global value for |
5235 // ":set" on local options. Note: when setting 'syntax' | 5236 // ":set" on local options. Note: when setting 'syntax' |
5236 // or 'filetype' autocommands may be triggered that can | 5237 // or 'filetype' autocommands may be triggered that can |
5237 // cause havoc. | 5238 // cause havoc. |
5238 errmsg = did_set_string_option(opt_idx, (char_u **)varp, | 5239 errmsg = did_set_string_option(opt_idx, (char_u **)varp, |
5239 new_value_alloced, oldval, errbuf, opt_flags); | 5240 new_value_alloced, oldval, errbuf, |
5241 opt_flags, &value_checked); | |
5240 | 5242 |
5241 if (did_inc_secure) | 5243 if (did_inc_secure) |
5242 --secure; | 5244 --secure; |
5243 } | 5245 } |
5244 | 5246 |
5278 redraw_all_later(CLEAR); | 5280 redraw_all_later(CLEAR); |
5279 } | 5281 } |
5280 } | 5282 } |
5281 | 5283 |
5282 if (opt_idx >= 0) | 5284 if (opt_idx >= 0) |
5283 did_set_option(opt_idx, opt_flags, value_is_replaced); | 5285 did_set_option( |
5286 opt_idx, opt_flags, value_is_replaced, value_checked); | |
5284 } | 5287 } |
5285 | 5288 |
5286 skip: | 5289 skip: |
5287 /* | 5290 /* |
5288 * Advance to next argument. | 5291 * Advance to next argument. |
5346 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag. | 5349 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag. |
5347 */ | 5350 */ |
5348 static void | 5351 static void |
5349 did_set_option( | 5352 did_set_option( |
5350 int opt_idx, | 5353 int opt_idx, |
5351 int opt_flags, /* possibly with OPT_MODELINE */ | 5354 int opt_flags, // possibly with OPT_MODELINE |
5352 int new_value) /* value was replaced completely */ | 5355 int new_value, // value was replaced completely |
5356 int value_checked) // value was checked to be safe, no need to set the | |
5357 // P_INSECURE flag. | |
5353 { | 5358 { |
5354 long_u *p; | 5359 long_u *p; |
5355 | 5360 |
5356 options[opt_idx].flags |= P_WAS_SET; | 5361 options[opt_idx].flags |= P_WAS_SET; |
5357 | 5362 |
5358 /* When an option is set in the sandbox, from a modeline or in secure mode | 5363 /* When an option is set in the sandbox, from a modeline or in secure mode |
5359 * set the P_INSECURE flag. Otherwise, if a new value is stored reset the | 5364 * set the P_INSECURE flag. Otherwise, if a new value is stored reset the |
5360 * flag. */ | 5365 * flag. */ |
5361 p = insecure_flag(opt_idx, opt_flags); | 5366 p = insecure_flag(opt_idx, opt_flags); |
5362 if (secure | 5367 if (!value_checked && (secure |
5363 #ifdef HAVE_SANDBOX | 5368 #ifdef HAVE_SANDBOX |
5364 || sandbox != 0 | 5369 || sandbox != 0 |
5365 #endif | 5370 #endif |
5366 || (opt_flags & OPT_MODELINE)) | 5371 || (opt_flags & OPT_MODELINE))) |
5367 *p = *p | P_INSECURE; | 5372 *p = *p | P_INSECURE; |
5368 else if (new_value) | 5373 else if (new_value) |
5369 *p = *p & ~P_INSECURE; | 5374 *p = *p & ~P_INSECURE; |
5370 } | 5375 } |
5371 | 5376 |
6034 #if defined(FEAT_EVAL) | 6039 #if defined(FEAT_EVAL) |
6035 char_u *saved_oldval = NULL; | 6040 char_u *saved_oldval = NULL; |
6036 char_u *saved_newval = NULL; | 6041 char_u *saved_newval = NULL; |
6037 #endif | 6042 #endif |
6038 char_u *r = NULL; | 6043 char_u *r = NULL; |
6044 int value_checked = FALSE; | |
6039 | 6045 |
6040 if (options[opt_idx].var == NULL) /* don't set hidden option */ | 6046 if (options[opt_idx].var == NULL) /* don't set hidden option */ |
6041 return NULL; | 6047 return NULL; |
6042 | 6048 |
6043 s = vim_strsave(value); | 6049 s = vim_strsave(value); |
6061 saved_oldval = vim_strsave(oldval); | 6067 saved_oldval = vim_strsave(oldval); |
6062 saved_newval = vim_strsave(s); | 6068 saved_newval = vim_strsave(s); |
6063 } | 6069 } |
6064 #endif | 6070 #endif |
6065 if ((r = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL, | 6071 if ((r = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL, |
6066 opt_flags)) == NULL) | 6072 opt_flags, &value_checked)) == NULL) |
6067 did_set_option(opt_idx, opt_flags, TRUE); | 6073 did_set_option(opt_idx, opt_flags, TRUE, value_checked); |
6068 | 6074 |
6069 #if defined(FEAT_EVAL) | 6075 #if defined(FEAT_EVAL) |
6070 /* call autocommand after handling side effects */ | 6076 /* call autocommand after handling side effects */ |
6071 if (r == NULL) | 6077 if (r == NULL) |
6072 trigger_optionsset_string(opt_idx, opt_flags, | 6078 trigger_optionsset_string(opt_idx, opt_flags, |
6097 * Handle string options that need some action to perform when changed. | 6103 * Handle string options that need some action to perform when changed. |
6098 * Returns NULL for success, or an error message for an error. | 6104 * Returns NULL for success, or an error message for an error. |
6099 */ | 6105 */ |
6100 static char_u * | 6106 static char_u * |
6101 did_set_string_option( | 6107 did_set_string_option( |
6102 int opt_idx, /* index in options[] table */ | 6108 int opt_idx, // index in options[] table |
6103 char_u **varp, /* pointer to the option variable */ | 6109 char_u **varp, // pointer to the option variable |
6104 int new_value_alloced, /* new value was allocated */ | 6110 int new_value_alloced, // new value was allocated |
6105 char_u *oldval, /* previous value of the option */ | 6111 char_u *oldval, // previous value of the option |
6106 char_u *errbuf, /* buffer for errors, or NULL */ | 6112 char_u *errbuf, // buffer for errors, or NULL |
6107 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */ | 6113 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL |
6114 int *value_checked) // value was checked to be save, no | |
6115 // need to set P_INSECURE | |
6108 { | 6116 { |
6109 char_u *errmsg = NULL; | 6117 char_u *errmsg = NULL; |
6110 char_u *s, *p; | 6118 char_u *s, *p; |
6111 int did_chartab = FALSE; | 6119 int did_chartab = FALSE; |
6112 char_u **gvarp; | 6120 char_u **gvarp; |
6132 ) && (options[opt_idx].flags & P_SECURE)) | 6140 ) && (options[opt_idx].flags & P_SECURE)) |
6133 { | 6141 { |
6134 errmsg = e_secure; | 6142 errmsg = e_secure; |
6135 } | 6143 } |
6136 | 6144 |
6137 /* Check for a "normal" directory or file name in some options. Disallow a | 6145 // Check for a "normal" directory or file name in some options. Disallow a |
6138 * path separator (slash and/or backslash), wildcards and characters that | 6146 // path separator (slash and/or backslash), wildcards and characters that |
6139 * are often illegal in a file name. Be more permissive if "secure" is off. | 6147 // are often illegal in a file name. Be more permissive if "secure" is off. |
6140 */ | |
6141 else if (((options[opt_idx].flags & P_NFNAME) | 6148 else if (((options[opt_idx].flags & P_NFNAME) |
6142 && vim_strpbrk(*varp, (char_u *)(secure | 6149 && vim_strpbrk(*varp, (char_u *)(secure |
6143 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL) | 6150 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL) |
6144 || ((options[opt_idx].flags & P_NDNAME) | 6151 || ((options[opt_idx].flags & P_NDNAME) |
6145 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL)) | 6152 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL)) |
6522 else if (varp == &curbuf->b_p_keymap) | 6529 else if (varp == &curbuf->b_p_keymap) |
6523 { | 6530 { |
6524 if (!valid_filetype(*varp)) | 6531 if (!valid_filetype(*varp)) |
6525 errmsg = e_invarg; | 6532 errmsg = e_invarg; |
6526 else | 6533 else |
6527 /* load or unload key mapping tables */ | 6534 { |
6535 int secure_save = secure; | |
6536 | |
6537 // Reset the secure flag, since the value of 'keymap' has | |
6538 // been checked to be safe. | |
6539 secure = 0; | |
6540 | |
6541 // load or unload key mapping tables | |
6528 errmsg = keymap_init(); | 6542 errmsg = keymap_init(); |
6543 | |
6544 secure = secure_save; | |
6545 | |
6546 // Since we check the value, there is no need to set P_INSECURE, | |
6547 // even when the value comes from a modeline. | |
6548 *value_checked = TRUE; | |
6549 } | |
6529 | 6550 |
6530 if (errmsg == NULL) | 6551 if (errmsg == NULL) |
6531 { | 6552 { |
6532 if (*curbuf->b_p_keymap != NUL) | 6553 if (*curbuf->b_p_keymap != NUL) |
6533 { | 6554 { |
7521 else if (gvarp == &p_ft) | 7542 else if (gvarp == &p_ft) |
7522 { | 7543 { |
7523 if (!valid_filetype(*varp)) | 7544 if (!valid_filetype(*varp)) |
7524 errmsg = e_invarg; | 7545 errmsg = e_invarg; |
7525 else | 7546 else |
7547 { | |
7526 value_changed = STRCMP(oldval, *varp) != 0; | 7548 value_changed = STRCMP(oldval, *varp) != 0; |
7549 | |
7550 // Since we check the value, there is no need to set P_INSECURE, | |
7551 // even when the value comes from a modeline. | |
7552 *value_checked = TRUE; | |
7553 } | |
7527 } | 7554 } |
7528 | 7555 |
7529 #ifdef FEAT_SYN_HL | 7556 #ifdef FEAT_SYN_HL |
7530 else if (gvarp == &p_syn) | 7557 else if (gvarp == &p_syn) |
7531 { | 7558 { |
7532 if (!valid_filetype(*varp)) | 7559 if (!valid_filetype(*varp)) |
7533 errmsg = e_invarg; | 7560 errmsg = e_invarg; |
7534 else | 7561 else |
7562 { | |
7535 value_changed = STRCMP(oldval, *varp) != 0; | 7563 value_changed = STRCMP(oldval, *varp) != 0; |
7564 | |
7565 // Since we check the value, there is no need to set P_INSECURE, | |
7566 // even when the value comes from a modeline. | |
7567 *value_checked = TRUE; | |
7568 } | |
7536 } | 7569 } |
7537 #endif | 7570 #endif |
7538 | 7571 |
7539 #ifdef FEAT_TERMINAL | 7572 #ifdef FEAT_TERMINAL |
7540 /* 'termwinkey' */ | 7573 /* 'termwinkey' */ |
7750 /* 'filetype' is set, trigger the FileType autocommand. | 7783 /* 'filetype' is set, trigger the FileType autocommand. |
7751 * Skip this when called from a modeline and the filetype was | 7784 * Skip this when called from a modeline and the filetype was |
7752 * already set to this value. */ | 7785 * already set to this value. */ |
7753 if (!(opt_flags & OPT_MODELINE) || value_changed) | 7786 if (!(opt_flags & OPT_MODELINE) || value_changed) |
7754 { | 7787 { |
7755 static int ft_recursive = 0; | 7788 static int ft_recursive = 0; |
7789 int secure_save = secure; | |
7790 | |
7791 // Reset the secure flag, since the value of 'filetype' has | |
7792 // been checked to be safe. | |
7793 secure = 0; | |
7756 | 7794 |
7757 ++ft_recursive; | 7795 ++ft_recursive; |
7758 did_filetype = TRUE; | 7796 did_filetype = TRUE; |
7759 // Only pass TRUE for "force" when the value changed or not | 7797 // Only pass TRUE for "force" when the value changed or not |
7760 // used recursively, to avoid endless recurrence. | 7798 // used recursively, to avoid endless recurrence. |
7762 value_changed || ft_recursive == 1, curbuf); | 7800 value_changed || ft_recursive == 1, curbuf); |
7763 --ft_recursive; | 7801 --ft_recursive; |
7764 /* Just in case the old "curbuf" is now invalid. */ | 7802 /* Just in case the old "curbuf" is now invalid. */ |
7765 if (varp != &(curbuf->b_p_ft)) | 7803 if (varp != &(curbuf->b_p_ft)) |
7766 varp = NULL; | 7804 varp = NULL; |
7805 | |
7806 secure = secure_save; | |
7767 } | 7807 } |
7768 } | 7808 } |
7769 #ifdef FEAT_SPELL | 7809 #ifdef FEAT_SPELL |
7770 if (varp == &(curwin->w_s->b_p_spl)) | 7810 if (varp == &(curwin->w_s->b_p_spl)) |
7771 { | 7811 { |