From 35595dbffdf413694b31da46129432ef345921af Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Thu, 25 Sep 2014 18:20:03 -0700 Subject: [PATCH] Make escape() return a wcstring This avoids the potential for leaking the resulting string. --- common.cpp | 8 ++++---- common.h | 4 ++-- input.cpp | 8 ++++---- kill.cpp | 16 ++++++---------- output.cpp | 9 +++------ reader.cpp | 6 ++---- wildcard.cpp | 5 ++--- 7 files changed, 23 insertions(+), 33 deletions(-) diff --git a/common.cpp b/common.cpp index 7429fcbe6..825d25015 100644 --- a/common.cpp +++ b/common.cpp @@ -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) diff --git a/common.h b/common.h index 7932c8700..a5ec50abe 100644 --- a/common.h +++ b/common.h @@ -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); /** diff --git a/input.cpp b/input.cpp index ed546b521..c909f5ab1 100644 --- a/input.cpp +++ b/input.cpp @@ -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) diff --git a/kill.cpp b/kill.cpp index 6ae27c519..0033ca2fc 100644 --- a/kill.cpp +++ b/kill.cpp @@ -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(); } diff --git a/output.cpp b/output.cpp index 2c9f7d486..aea0a9073 100644 --- a/output.cpp +++ b/output.cpp @@ -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; } diff --git a/reader.cpp b/reader.cpp index 795b5fcc5..4f3cb7b5e 100644 --- a/reader.cpp +++ b/reader.cpp @@ -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 { diff --git a/wildcard.cpp b/wildcard.cpp index 1aa930fc9..b2a610925 100644 --- a/wildcard.cpp +++ b/wildcard.cpp @@ -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::iterator iter = suffix_map.find(suff); wcstring desc;