vim-patch:8.1.0211: expanding a file name "~" results in $HOME

Problem:    Expanding a file name "~" results in $HOME. (Aidan Shafran)
Solution:   Change "~" to "./~" before expanding. (closes vim/vim#3072)
00136dc321
This commit is contained in:
Jan Edmund Lazo
2019-05-25 17:22:19 -04:00
parent 55419a6904
commit 08aa9b0023
5 changed files with 30 additions and 9 deletions

View File

@@ -8868,8 +8868,8 @@ static void f_fnamemodify(typval_T *argvars, typval_T *rettv, FunPtr fptr)
} else {
len = strlen(fname);
size_t usedlen = 0;
(void)modify_fname((char_u *)mods, &usedlen, (char_u **)&fname, &fbuf,
&len);
(void)modify_fname((char_u *)mods, false, &usedlen,
(char_u **)&fname, &fbuf, &len);
}
rettv->v_type = VAR_STRING;
@@ -22623,6 +22623,7 @@ void reset_v_option_vars(void)
int
modify_fname(
char_u *src, // string with modifiers
bool tilde_file, // "~" is a file name, not $HOME
size_t *usedlen, // characters after src that are used
char_u **fnamep, // file name so far
char_u **bufp, // buffer for allocated file name or NULL
@@ -22652,8 +22653,8 @@ repeat:
|| (*fnamep)[1] == '\\'
# endif
|| (*fnamep)[1] == NUL)
#endif
&& !(tilde_file && (*fnamep)[1] == NUL)
) {
*fnamep = expand_env_save(*fnamep);
xfree(*bufp); /* free any allocated file name */

View File

@@ -8552,6 +8552,7 @@ eval_vars (
size_t resultlen;
buf_T *buf;
int valid = VALID_HEAD | VALID_PATH; // Assume valid result.
bool tilde_file = false;
int skip_mod = false;
char strbuf[30];
@@ -8609,8 +8610,10 @@ eval_vars (
if (curbuf->b_fname == NULL) {
result = (char_u *)"";
valid = 0; /* Must have ":p:h" to be valid */
} else
} else {
result = curbuf->b_fname;
tilde_file = STRCMP(result, "~") == 0;
}
break;
case SPEC_HASH: /* '#' or "#99": alternate file */
@@ -8660,8 +8663,10 @@ eval_vars (
if (buf->b_fname == NULL) {
result = (char_u *)"";
valid = 0; /* Must have ":p:h" to be valid */
} else
} else {
result = buf->b_fname;
tilde_file = STRCMP(result, "~") == 0;
}
}
break;
@@ -8746,7 +8751,8 @@ eval_vars (
resultlen = (size_t)(s - result);
}
} else if (!skip_mod) {
valid |= modify_fname(src, usedlen, &result, &resultbuf, &resultlen);
valid |= modify_fname(src, tilde_file, usedlen, &result,
&resultbuf, &resultlen);
if (result == NULL) {
*errormsg = (char_u *)"";
return NULL;

View File

@@ -425,7 +425,8 @@ cs_add_common(
expand_env((char_u *)arg1, (char_u *)fname, MAXPATHL);
size_t len = STRLEN(fname);
fbuf = (char_u *)fname;
(void)modify_fname((char_u *)":p", &usedlen, (char_u **)&fname, &fbuf, &len);
(void)modify_fname((char_u *)":p", false, &usedlen,
(char_u **)&fname, &fbuf, &len);
if (fname == NULL)
goto add_err;
fname = (char *)vim_strnsave((char_u *)fname, len);

View File

@@ -870,8 +870,8 @@ size_t home_replace(const buf_T *const buf, const char_u *src,
size_t usedlen = 0;
size_t flen = strlen(homedir_env_mod);
char_u *fbuf = NULL;
(void)modify_fname((char_u *)":p", &usedlen, (char_u **)&homedir_env_mod,
&fbuf, &flen);
(void)modify_fname((char_u *)":p", false, &usedlen,
(char_u **)&homedir_env_mod, &fbuf, &flen);
flen = strlen(homedir_env_mod);
assert(homedir_env_mod != homedir_env);
if (vim_ispathsep(homedir_env_mod[flen - 1])) {

View File

@@ -62,6 +62,14 @@ describe('expand file name', function()
call delete('Xdir ~ dir', 'd')
call assert_false(isdirectory('Xdir ~ dir'))
endfunc
func Test_expand_tilde_filename()
split ~
call assert_equal('~', expand('%'))
call assert_notequal(expand('%:p'), expand('~/'))
call assert_match('\~', expand('%:p'))
bwipe!
endfunc
]])
end)
@@ -74,4 +82,9 @@ describe('expand file name', function()
call('Test_with_tilde')
expected_empty()
end)
it('does not expand tilde if it is a filename', function()
call('Test_expand_tilde_filename')
expected_empty()
end)
end)