[string] Allow url/var escaping to handle NULs

Fixes #4605.
This commit is contained in:
Fabian Homborg 2018-01-03 12:29:01 +01:00
parent 485fdbde41
commit 4dc1c6ca39
2 changed files with 9 additions and 11 deletions

View File

@ -517,8 +517,8 @@ static int string_escape_url(options_t &opts, int optind, wchar_t **argv, io_str
escape_flags_t flags = 0;
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
streams.out.append(escape_string(arg, flags, STRING_STYLE_URL));
while (auto arg = aiter.nextstr()) {
streams.out.append(escape_string(*arg, flags, STRING_STYLE_URL));
streams.out.append(L'\n');
nesc++;
}
@ -533,8 +533,8 @@ static int string_escape_var(options_t &opts, int optind, wchar_t **argv, io_str
escape_flags_t flags = 0;
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
streams.out.append(escape_string(arg, flags, STRING_STYLE_VAR));
while (auto arg = aiter.nextstr()) {
streams.out.append(escape_string(*arg, flags, STRING_STYLE_VAR));
streams.out.append(L'\n');
nesc++;
}

View File

@ -804,9 +804,8 @@ wcstring reformat_for_screen(const wcstring &msg) {
}
/// Escape a string in a fashion suitable for using as a URL. Store the result in out_str.
static void escape_string_url(const wchar_t *orig_in, wcstring &out) {
const std::string &in = wcs2string(orig_in);
for (auto c1 : in) {
static void escape_string_url(wcstring in, wcstring &out) {
for (auto& c1 : in) {
// This silliness is so we get the correct result whether chars are signed or unsigned.
unsigned int c2 = (unsigned int)c1 & 0xFF;
if (!(c2 & 0x80) &&
@ -855,9 +854,8 @@ static bool unescape_string_url(const wchar_t *in, wcstring *out) {
}
/// Escape a string in a fashion suitable for using as a fish var name. Store the result in out_str.
static void escape_string_var(const wchar_t *orig_in, wcstring &out) {
static void escape_string_var(const wcstring in, wcstring &out) {
bool prev_was_hex_encoded = false;
const std::string &in = wcs2string(orig_in);
for (auto c1 : in) {
// This silliness is so we get the correct result whether chars are signed or unsigned.
unsigned int c2 = (unsigned int)c1 & 0xFF;
@ -1109,11 +1107,11 @@ wcstring escape_string(const wcstring &in, escape_flags_t flags, escape_string_s
break;
}
case STRING_STYLE_URL: {
escape_string_url(in.c_str(), result);
escape_string_url(in, result);
break;
}
case STRING_STYLE_VAR: {
escape_string_var(in.c_str(), result);
escape_string_var(in, result);
break;
}
}