]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/escape: always escape newlines in shell_escape()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 3 Mar 2021 12:47:55 +0000 (13:47 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 5 May 2021 10:12:42 +0000 (12:12 +0200)
shell_escape() is mostly used for mount paths and similar, where we assume
no newlines are present in the string. But if any were ever present, we
should escape them. So let's simplify the code by making this unconditional.

src/basic/escape.c
src/test/test-escape.c

index 2de275f04e1f2f121bbcff09d1042eb2d5254913..f45536d9bdead8af6a84b368170bc89925c4ec9d 100644 (file)
@@ -462,11 +462,11 @@ char* octescape(const char *s, size_t len) {
 
 }
 
-static char* strcpy_backslash_escaped(char *t, const char *s, const char *bad, bool escape_tab_nl) {
+static char* strcpy_backslash_escaped(char *t, const char *s, const char *bad) {
         assert(bad);
 
         for (; *s; s++) {
-                if (escape_tab_nl && IN_SET(*s, '\n', '\t')) {
+                if (IN_SET(*s, '\n', '\t')) {
                         *(t++) = '\\';
                         *(t++) = *s == '\n' ? 'n' : 't';
                         continue;
@@ -488,7 +488,7 @@ char* shell_escape(const char *s, const char *bad) {
         if (!r)
                 return NULL;
 
-        t = strcpy_backslash_escaped(r, s, bad, false);
+        t = strcpy_backslash_escaped(r, s, bad);
         *t = 0;
 
         return r;
@@ -530,8 +530,7 @@ char* shell_maybe_quote(const char *s, ShellEscapeFlags flags) {
         t = mempcpy(t, s, p - s);
 
         t = strcpy_backslash_escaped(t, p,
-                                     FLAGS_SET(flags, SHELL_ESCAPE_POSIX) ? SHELL_NEED_ESCAPE_POSIX : SHELL_NEED_ESCAPE,
-                                     true);
+                                     FLAGS_SET(flags, SHELL_ESCAPE_POSIX) ? SHELL_NEED_ESCAPE_POSIX : SHELL_NEED_ESCAPE);
 
         if (FLAGS_SET(flags, SHELL_ESCAPE_POSIX))
                 *(t++) = '\'';
index 854a55174b98b1bcb8d0812e1a01f3b89b5945ab..848aec37d8cd93dd19a77b1e5e2789897edb1256 100644 (file)
@@ -127,6 +127,7 @@ static void test_shell_escape(void) {
         test_shell_escape_one("foobar", "", "foobar");
         test_shell_escape_one("foobar", "o", "f\\o\\obar");
         test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
+        test_shell_escape_one("foo\nbar\nbaz", ",:", "foo\\nbar\\nbaz");
 }
 
 static void test_shell_maybe_quote_one(const char *s, ShellEscapeFlags flags, const char *expected) {