From 895f712df8afc053b59783189954e665ccd21dab Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 25 Jun 2016 23:39:16 +0300 Subject: option: Do not expand options, obtained from XDG vars It is a wrong thing to do, this makes valid variable values be treated incorrectly: in XDG_DATA_HOME='/home/$foo/.local/share' `$foo` should be treated literally and not expanded to `foo` environment variable value. Also makes option_expand not try to expand too long strings even if these too long strings are default values. Previously it thought that default values should always be expanded. Also does not try to expand NULL should it be the default value just in case. Fixes #4961 --- src/nvim/option.c | 18 ++++++++++++------ src/nvim/options.lua | 10 +++++----- 2 files changed, 17 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/nvim/option.c b/src/nvim/option.c index 81f57522b3..e75cb38c9e 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -236,6 +236,7 @@ typedef struct vimoption { #define P_NO_ML 0x2000000U ///< not allowed in modeline #define P_CURSWANT 0x4000000U ///< update curswant required; not needed ///< when there is a redraw flag +#define P_NO_DEF_EXP 0x8000000U ///< Do not expand default value. #define HIGHLIGHT_INIT \ "8:SpecialKey,~:EndOfBuffer,z:TermCursor,Z:TermCursorNC,@:NonText," \ @@ -726,6 +727,9 @@ void set_init_1(void) * default. */ for (opt_idx = 0; options[opt_idx].fullname; opt_idx++) { + if (options[opt_idx].flags & P_NO_DEF_EXP) { + continue; + } char *p; if ((options[opt_idx].flags & P_GETTEXT) && options[opt_idx].var != NULL) { @@ -2020,13 +2024,15 @@ static char_u *option_expand(int opt_idx, char_u *val) if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL) return NULL; - /* If val is longer than MAXPATHL no meaningful expansion can be done, - * expand_env() would truncate the string. */ - if (val != NULL && STRLEN(val) > MAXPATHL) - return NULL; - - if (val == NULL) + if (val == NULL) { val = *(char_u **)options[opt_idx].var; + } + + // If val is longer than MAXPATHL no meaningful expansion can be done, + // expand_env() would truncate the string. + if (val == NULL || STRLEN(val) > MAXPATHL) { + return NULL; + } /* * Expanding this with NameBuff, expand_env() must not be passed IObuff. diff --git a/src/nvim/options.lua b/src/nvim/options.lua index d19af4f73f..060ec8c1e1 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -165,7 +165,7 @@ return { deny_duplicates=true, secure=true, vi_def=true, - expand=true, + expand='nodefault', varname='p_bdir', defaults={if_true={vi=''}} }, @@ -616,7 +616,7 @@ return { deny_duplicates=true, secure=true, vi_def=true, - expand=true, + expand='nodefault', varname='p_dir', defaults={if_true={vi=''}} }, @@ -1891,7 +1891,7 @@ return { deny_duplicates=true, secure=true, vi_def=true, - expand=true, + expand='nodefault', varname='p_rtp', defaults={if_true={vi=''}} }, @@ -2507,7 +2507,7 @@ return { deny_duplicates=true, secure=true, vi_def=true, - expand=true, + expand='nodefault', varname='p_udir', defaults={if_true={vi=''}} }, @@ -2568,7 +2568,7 @@ return { type='string', scope={'global'}, secure=true, vi_def=true, - expand=true, + expand='nodefault', varname='p_vdir', defaults={if_true={vi=''}} }, -- cgit From c47624482c4ec23d055e30ecb3e24b04794f1235 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 26 Jun 2016 17:56:09 +0300 Subject: option: Also do not expand XDG defaults with set& --- src/nvim/option.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/nvim/option.c b/src/nvim/option.c index e75cb38c9e..481e7daa0f 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1477,15 +1477,18 @@ do_set ( * default value was already expanded, only * required when an environment variable was set * later */ - if (newval == NULL) + new_value_alloced = true; + if (newval == NULL) { newval = empty_option; - else { + } else if (!(options[opt_idx].flags | P_NO_DEF_EXP)) { s = option_expand(opt_idx, newval); - if (s == NULL) + if (s == NULL) { s = newval; + } newval = vim_strsave(s); + } else { + newval = (char_u *)xstrdup((char *)newval); } - new_value_alloced = TRUE; } else if (nextchar == '<') { /* set to global val */ newval = vim_strsave(*(char_u **)get_varp_scope( &(options[opt_idx]), OPT_GLOBAL)); -- cgit From 1a92585e320812ae113a4d4ec6769b1978bedfe3 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 26 Jun 2016 17:56:25 +0300 Subject: option.h: Turn OPT_* list into a enum --- src/nvim/option.h | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/nvim/option.h b/src/nvim/option.h index 5c2b2662b5..3a43b859a8 100644 --- a/src/nvim/option.h +++ b/src/nvim/option.h @@ -6,17 +6,18 @@ #define BCO_ALWAYS 2 /* always copy the options */ #define BCO_NOHELP 4 /* don't touch the help related options */ -/* - * "flags" values for option-setting functions. - * When OPT_GLOBAL and OPT_LOCAL are both missing, set both local and global - * values, get local value. - */ -#define OPT_FREE 1 /* free old value if it was allocated */ -#define OPT_GLOBAL 2 /* use global value */ -#define OPT_LOCAL 4 /* use local value */ -#define OPT_MODELINE 8 /* option in modeline */ -#define OPT_WINONLY 16 /* only set window-local options */ -#define OPT_NOWIN 32 /* don't set window-local options */ +/// Flags for option-setting functions +/// +/// When OPT_GLOBAL and OPT_LOCAL are both missing, set both local and global +/// values, get local value. +typedef enum { + OPT_FREE = 1, ///< Free old value if it was allocated. + OPT_GLOBAL = 2, ///< Use global value. + OPT_LOCAL = 4, ///< Use local value. + OPT_MODELINE = 8, ///< Option in modeline. + OPT_WINONLY = 16, ///< Only set window-local options. + OPT_NOWIN = 32, ///< Don’t set window-local options. +} OptionFlags; #ifdef INCLUDE_GENERATED_DECLARATIONS # include "option.h.generated.h" -- cgit From 5fc72882ccf5aefbcfe96f6f895cc8046aa70923 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 26 Jun 2016 18:16:23 +0300 Subject: option: Also escape commas in options other then &runtimepath --- src/nvim/option.c | 11 +++++++---- src/nvim/os/stdpaths.c | 26 +++++++++++++++++++------- src/nvim/shada.c | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/nvim/option.c b/src/nvim/option.c index 481e7daa0f..85a4ed40ad 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -677,15 +677,18 @@ void set_init_1(void) #endif false); - char *backupdir = stdpaths_user_data_subpath("backup", 0); + char *backupdir = stdpaths_user_data_subpath("backup", 0, true); const size_t backupdir_len = strlen(backupdir); backupdir = xrealloc(backupdir, backupdir_len + 3); memmove(backupdir + 2, backupdir, backupdir_len + 1); memmove(backupdir, ".,", 2); - set_string_default("viewdir", stdpaths_user_data_subpath("view", 0), true); + set_string_default("viewdir", stdpaths_user_data_subpath("view", 0, true), + true); set_string_default("backupdir", backupdir, true); - set_string_default("directory", stdpaths_user_data_subpath("swap", 2), true); - set_string_default("undodir", stdpaths_user_data_subpath("undo", 0), true); + set_string_default("directory", stdpaths_user_data_subpath("swap", 2, true), + true); + set_string_default("undodir", stdpaths_user_data_subpath("undo", 0, true), + true); // Set default for &runtimepath. All necessary expansions are performed in // this function. set_runtimepath_default(); diff --git a/src/nvim/os/stdpaths.c b/src/nvim/os/stdpaths.c index 81ceb919c4..10b3f4c091 100644 --- a/src/nvim/os/stdpaths.c +++ b/src/nvim/os/stdpaths.c @@ -100,18 +100,30 @@ char *stdpaths_user_conf_subpath(const char *fname) /// /// @param[in] fname New component of the path. /// @param[in] trailing_pathseps Amount of trailing path separators to add. +/// @param[in] escape_commas If true, all commas will be escaped. /// -/// @return [allocated] `$XDG_DATA_HOME/nvim/{fname}` +/// @return [allocated] `$XDG_DATA_HOME/nvim/{fname}`. char *stdpaths_user_data_subpath(const char *fname, - const size_t trailing_pathseps) + const size_t trailing_pathseps, + const bool escape_commas) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET { char *ret = concat_fnames_realloc(get_xdg_home(kXDGDataHome), fname, true); - if (trailing_pathseps) { - const size_t len = strlen(ret); - ret = xrealloc(ret, len + trailing_pathseps + 1); - memset(ret + len, PATHSEP, trailing_pathseps); - ret[len + trailing_pathseps] = NUL; + const size_t len = strlen(ret); + const size_t numcommas = (escape_commas ? memcnt(ret, ',', len) : 0); + if (numcommas || trailing_pathseps) { + ret = xrealloc(ret, len + trailing_pathseps + numcommas + 1); + for (size_t i = 0 ; i < len + numcommas ; i++) { + if (ret[i] == ',') { + memmove(ret + i + 1, ret + i, len - i + numcommas); + ret[i] = '\\'; + i++; + } + } + if (trailing_pathseps) { + memset(ret + len + numcommas, PATHSEP, trailing_pathseps); + } + ret[len + trailing_pathseps + numcommas] = NUL; } return ret; } diff --git a/src/nvim/shada.c b/src/nvim/shada.c index b5921eb810..fe62f06e59 100644 --- a/src/nvim/shada.c +++ b/src/nvim/shada.c @@ -1524,7 +1524,7 @@ static const char *shada_get_default_file(void) FUNC_ATTR_WARN_UNUSED_RESULT { if (default_shada_file == NULL) { - char *shada_dir = stdpaths_user_data_subpath("shada", 0); + char *shada_dir = stdpaths_user_data_subpath("shada", 0, false); default_shada_file = concat_fnames_realloc(shada_dir, "main.shada", true); } return default_shada_file; -- cgit From 56fcabbd05b85388f2f5188fd1284bcff1ba67a2 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 26 Jun 2016 18:24:05 +0300 Subject: option: Silence linter --- src/nvim/option.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/option.c b/src/nvim/option.c index 85a4ed40ad..a7b44b372c 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1492,7 +1492,7 @@ do_set ( } else { newval = (char_u *)xstrdup((char *)newval); } - } else if (nextchar == '<') { /* set to global val */ + } else if (nextchar == '<') { // set to global val newval = vim_strsave(*(char_u **)get_varp_scope( &(options[opt_idx]), OPT_GLOBAL)); new_value_alloced = TRUE; -- cgit