mirror of
https://github.com/systemd/systemd
synced 2024-10-15 04:24:19 +00:00
Merge pull request #26628 from msizanoen1/utf8-quote-valid
escape: Ensure that output is always valid UTF-8
This commit is contained in:
commit
a41ac8ac40
|
@ -473,15 +473,23 @@ char* octescape(const char *s, size_t len) {
|
||||||
|
|
||||||
static char* strcpy_backslash_escaped(char *t, const char *s, const char *bad) {
|
static char* strcpy_backslash_escaped(char *t, const char *s, const char *bad) {
|
||||||
assert(bad);
|
assert(bad);
|
||||||
|
assert(t);
|
||||||
|
assert(s);
|
||||||
|
|
||||||
for (; *s; s++)
|
while (*s) {
|
||||||
if (char_is_cc(*s))
|
int l = utf8_encoded_valid_unichar(s, SIZE_MAX);
|
||||||
t += cescape_char(*s, t);
|
|
||||||
else {
|
if (char_is_cc(*s) || l < 0)
|
||||||
|
t += cescape_char(*(s++), t);
|
||||||
|
else if (l == 1) {
|
||||||
if (*s == '\\' || strchr(bad, *s))
|
if (*s == '\\' || strchr(bad, *s))
|
||||||
*(t++) = '\\';
|
*(t++) = '\\';
|
||||||
*(t++) = *s;
|
*(t++) = *(s++);
|
||||||
|
} else {
|
||||||
|
t = mempcpy(t, s, l);
|
||||||
|
s += l;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
@ -510,11 +518,16 @@ char* shell_maybe_quote(const char *s, ShellEscapeFlags flags) {
|
||||||
if (FLAGS_SET(flags, SHELL_ESCAPE_EMPTY) && isempty(s))
|
if (FLAGS_SET(flags, SHELL_ESCAPE_EMPTY) && isempty(s))
|
||||||
return strdup("\"\""); /* We don't use $'' here in the POSIX mode. "" is fine too. */
|
return strdup("\"\""); /* We don't use $'' here in the POSIX mode. "" is fine too. */
|
||||||
|
|
||||||
for (p = s; *p; p++)
|
for (p = s; *p; ) {
|
||||||
if (char_is_cc(*p) ||
|
int l = utf8_encoded_valid_unichar(p, SIZE_MAX);
|
||||||
|
|
||||||
|
if (char_is_cc(*p) || l < 0 ||
|
||||||
strchr(WHITESPACE SHELL_NEED_QUOTES, *p))
|
strchr(WHITESPACE SHELL_NEED_QUOTES, *p))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
p += l;
|
||||||
|
}
|
||||||
|
|
||||||
if (!*p)
|
if (!*p)
|
||||||
return strdup(s);
|
return strdup(s);
|
||||||
|
|
||||||
|
|
|
@ -196,6 +196,10 @@ TEST(shell_maybe_quote) {
|
||||||
|
|
||||||
test_shell_maybe_quote_one("głąb\002\003rząd", 0, "\"głąb\\002\\003rząd\"");
|
test_shell_maybe_quote_one("głąb\002\003rząd", 0, "\"głąb\\002\\003rząd\"");
|
||||||
test_shell_maybe_quote_one("głąb\002\003rząd", SHELL_ESCAPE_POSIX, "$'głąb\\002\\003rząd'");
|
test_shell_maybe_quote_one("głąb\002\003rząd", SHELL_ESCAPE_POSIX, "$'głąb\\002\\003rząd'");
|
||||||
|
|
||||||
|
/* Bogus UTF-8 strings */
|
||||||
|
test_shell_maybe_quote_one("\250\350", 0, "\"\\250\\350\"");
|
||||||
|
test_shell_maybe_quote_one("\250\350", SHELL_ESCAPE_POSIX, "$'\\250\\350'");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_quote_command_line_one(char **argv, const char *expected) {
|
static void test_quote_command_line_one(char **argv, const char *expected) {
|
||||||
|
|
Loading…
Reference in a new issue