Make escape() return a wcstring

This avoids the potential for leaking the resulting string.
This commit is contained in:
Kevin Ballard 2014-09-25 18:20:03 -07:00
parent cd4fa518b8
commit 35595dbffd
7 changed files with 23 additions and 33 deletions

View File

@ -1120,7 +1120,7 @@ static void escape_string_internal(const wchar_t *orig_in, size_t in_len, wcstri
}
}
wchar_t *escape(const wchar_t *in, escape_flags_t flags)
wcstring escape(const wchar_t *in, escape_flags_t flags)
{
if (!in)
{
@ -1128,9 +1128,9 @@ wchar_t *escape(const wchar_t *in, escape_flags_t flags)
FATAL_EXIT();
}
wcstring tmp;
escape_string_internal(in, wcslen(in), &tmp, flags);
return wcsdup(tmp.c_str());
wcstring result;
escape_string_internal(in, wcslen(in), &result, flags);
return result;
}
wcstring escape_string(const wcstring &in, escape_flags_t flags)

View File

@ -808,10 +808,10 @@ void print_stderr(const wcstring &str);
\param in The string to be escaped
\param flags Flags to control the escaping
\return The escaped string, or 0 if there is not enough memory
\return The escaped string
*/
wchar_t *escape(const wchar_t *in, escape_flags_t flags);
wcstring escape(const wchar_t *in, escape_flags_t flags);
wcstring escape_string(const wcstring &in, escape_flags_t flags);
/**

View File

@ -365,7 +365,7 @@ void input_mapping_add(const wchar_t *sequence, const wchar_t **commands, size_t
CHECK(mode,);
CHECK(sets_mode,);
// debug( 0, L"Add mapping from %ls to %ls in mode %ls", escape(sequence, 1), escape(command, 1 ), mode);
// debug( 0, L"Add mapping from %ls to %ls in mode %ls", escape(sequence, ESCAPE_ALL).c_str(), escape(command, ESCAPE_ALL).c_str(), mode);
// remove existing mappings with this sequence
const wcstring_list_t commands_vector(commands, commands + commands_len);
@ -629,7 +629,7 @@ static bool input_mapping_is_match(const input_mapping_t &m)
wint_t c = 0;
int j;
//debug(0, L"trying mapping %ls\n", escape(m.seq.c_str(), 1));
//debug(0, L"trying mapping %ls\n", escape(m.seq.c_str(), ESCAPE_ALL).c_str());
const wchar_t *str = m.seq.c_str();
for (j=0; str[j] != L'\0'; j++)
{
@ -644,7 +644,7 @@ static bool input_mapping_is_match(const input_mapping_t &m)
if (str[j] == L'\0')
{
//debug(0, L"matched mapping %ls (%ls)\n", escape(m.seq.c_str(), 1), m.command.c_str());
//debug(0, L"matched mapping %ls (%ls)\n", escape(m.seq.c_str(), ESCAPE_ALL).c_str(), m.command.c_str());
/* We matched the entire sequence */
return true;
}
@ -680,7 +680,7 @@ static void input_mapping_execute_matching_or_generic(bool allow_commands)
{
const input_mapping_t &m = mapping_list.at(i);
//debug(0, L"trying mapping (%ls,%ls,%ls)\n", escape(m.seq.c_str(), 1),
//debug(0, L"trying mapping (%ls,%ls,%ls)\n", escape(m.seq.c_str(), ESCAPE_ALL).c_str(),
// m.mode.c_str(), m.sets_mode.c_str());
if (m.mode != bind_mode)

View File

@ -49,7 +49,7 @@ static kill_list_t kill_list;
/**
Contents of the X clipboard, at last time we checked it
*/
static wchar_t *cut_buffer=0;
static wcstring cut_buffer;
/**
Test if the xsel command is installed. Since this is called often,
@ -73,7 +73,7 @@ void kill_add(const wcstring &str)
return;
wcstring cmd;
wchar_t *escaped_str = NULL;
wcstring escaped_str;
kill_list.push_front(str);
/*
@ -87,7 +87,7 @@ void kill_add(const wcstring &str)
const env_var_t clipboard_wstr = env_get_string(L"FISH_CLIPBOARD_CMD");
if (!clipboard_wstr.missing())
{
escaped_str = escape(str.c_str(), 1);
escaped_str = escape(str.c_str(), ESCAPE_ALL);
cmd.assign(L"echo -n ");
cmd.append(escaped_str);
cmd.append(clipboard_wstr);
@ -119,8 +119,6 @@ void kill_add(const wcstring &str)
*/
}
free(cut_buffer);
cut_buffer = escaped_str;
}
}
@ -193,10 +191,9 @@ static void kill_check_x_buffer()
etc. anyway.
*/
if (cut_buffer == NULL || cut_buffer != new_cut_buffer)
if (cut_buffer != new_cut_buffer)
{
free(cut_buffer);
cut_buffer = wcsdup(new_cut_buffer.c_str());
cut_buffer = new_cut_buffer;
kill_list.push_front(new_cut_buffer);
}
}
@ -228,7 +225,6 @@ void kill_init()
void kill_destroy()
{
if (cut_buffer)
free(cut_buffer);
cut_buffer.clear();
}

View File

@ -567,15 +567,13 @@ void writestr_ellipsis(const wchar_t *str, int max_width)
int write_escaped_str(const wchar_t *str, int max_len)
{
wchar_t *out;
int i;
int len;
int written=0;
CHECK(str, 0);
out = escape(str, 1);
len = fish_wcswidth(out);
wcstring out = escape(str, ESCAPE_ALL);
int len = fish_wcswidth(out);
if (max_len && (max_len < len))
{
@ -596,10 +594,9 @@ int write_escaped_str(const wchar_t *str, int max_len)
else
{
written = len;
writestr(out);
writestr(out.c_str());
}
free(out);
return written;
}

View File

@ -1309,7 +1309,6 @@ wcstring completion_apply_to_command_line(const wcstring &val_str, complete_flag
{
size_t move_cursor;
const wchar_t *begin, *end;
wchar_t *escaped;
const wchar_t *buff = command_line.c_str();
parse_util_token_extent(buff, cursor_pos, &begin, 0, 0, 0);
@ -1321,10 +1320,9 @@ wcstring completion_apply_to_command_line(const wcstring &val_str, complete_flag
{
/* Respect COMPLETE_DONT_ESCAPE_TILDES */
bool no_tilde = !!(flags & COMPLETE_DONT_ESCAPE_TILDES);
escaped = escape(val, ESCAPE_ALL | ESCAPE_NO_QUOTED | (no_tilde ? ESCAPE_NO_TILDE : 0));
wcstring escaped = escape(val, ESCAPE_ALL | ESCAPE_NO_QUOTED | (no_tilde ? ESCAPE_NO_TILDE : 0));
sb.append(escaped);
move_cursor = wcslen(escaped);
free(escaped);
move_cursor = escaped.size();
}
else
{

View File

@ -440,7 +440,6 @@ static wcstring complete_get_desc_suffix(const wchar_t *suff_orig)
size_t len;
wchar_t *suff;
wchar_t *pos;
wchar_t *tmp;
len = wcslen(suff_orig);
@ -461,9 +460,9 @@ static wcstring complete_get_desc_suffix(const wchar_t *suff_orig)
}
}
tmp = escape(suff, 1);
wcstring tmp = escape(suff, ESCAPE_ALL);
free(suff);
suff = tmp;
suff = wcsdup(tmp.c_str());
std::map<wcstring, wcstring>::iterator iter = suffix_map.find(suff);
wcstring desc;