Don't crash when autosuggesting cd'ing into a directory with ~

Fixes #2696
This commit is contained in:
ridiculousfish 2016-02-02 16:46:57 -08:00
parent f8da754884
commit 60a477a303
2 changed files with 14 additions and 4 deletions

View file

@ -2382,6 +2382,16 @@ static void test_autosuggest_suggest_special()
// A single quote should defeat tilde expansion
perform_one_autosuggestion_special_test(L"cd '~/test_autosuggest_suggest_specia'", wd, L"", __LINE__);
// Don't crash on ~ (2696)
// note this was wd dependent, hence why we set it
char saved_wd[PATH_MAX] = {};
if (NULL == getcwd(saved_wd, sizeof saved_wd)) err(L"getcwd failed");
if (chdir("/tmp/autosuggest_test/")) err(L"chdir failed");
if (system("mkdir -p '/tmp/autosuggest_test/~hahaha/path1/path2/'")) err(L"mkdir failed");
perform_one_autosuggestion_special_test(L"cd ~haha", wd, L"cd ~hahaha/path1/path2/", __LINE__);
if (chdir(saved_wd)) err(L"chdir failed");
if (system("rm -Rf '/tmp/autosuggest_test/'")) err(L"rm failed");
if (system("rm -Rf ~/test_autosuggest_suggest_special/")) err(L"rm failed");
}

View file

@ -65,7 +65,7 @@ static const wchar_t * const highlight_var[] =
};
/* If the given path looks like it's relative to the working directory, then prepend that working directory. */
/* If the given path looks like it's relative to the working directory, then prepend that working directory. This operates on unescaped paths only (so a ~ means a literal ~) */
static wcstring apply_working_directory(const wcstring &path, const wcstring &working_directory)
{
if (path.empty() || working_directory.empty())
@ -76,7 +76,7 @@ static wcstring apply_working_directory(const wcstring &path, const wcstring &wo
switch (path.at(0))
{
case L'/':
case L'~':
case HOME_DIRECTORY:
prepend_wd = false;
break;
default:
@ -1218,7 +1218,7 @@ void highlighter_t::color_redirection(const parse_node_t &redirection_node)
}
else
{
/* Ok, we successfully expanded our target. Now verify that it works with this redirection. We will probably need it as a path (but not in the case of fd redirections */
/* Ok, we successfully expanded our target. Now verify that it works with this redirection. We will probably need it as a path (but not in the case of fd redirections). Note that the target is now unescaped. */
const wcstring target_path = apply_working_directory(target, this->working_directory);
switch (redirect_type)
{