]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
env-file: bring our decoding of double-quoted strings in env files in line with shell
authorLennart Poettering <lennart@poettering.net>
Fri, 11 Sep 2020 13:14:03 +0000 (15:14 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 14 Sep 2020 09:08:43 +0000 (11:08 +0200)
In shell, inside of double quotes only a select few chars should be
escaped. If other chars are escaped this has no effect. Correct the list
of chars that need such escaping.

Also, make sure we can read back the stuff we wrote out without loss.

Fixes: #16788
src/basic/env-file.c

index 26470796eb9acf5169031df717ac75b6282441b3..dc92b13a6f922305fad661d752d8e9d1c41f03c1 100644 (file)
@@ -209,17 +209,21 @@ static int parse_env_file_internal(
                 case DOUBLE_QUOTE_VALUE_ESCAPE:
                         state = DOUBLE_QUOTE_VALUE;
 
-                        if (c == '"') {
+                        if (strchr(SHELL_NEED_ESCAPE, c)) {
+                                /* If this is a char that needs escaping, just unescape it. */
                                 if (!GREEDY_REALLOC(value, value_alloc, n_value+2))
                                         return -ENOMEM;
-                                value[n_value++] = '"';
-                        } else if (!strchr(NEWLINE, c)) {
+                                value[n_value++] = c;
+                        } else if (c != '\n') {
+                                /* If other char than what needs escaping, keep the "\" in place, like the
+                                 * real shell does. */
                                 if (!GREEDY_REALLOC(value, value_alloc, n_value+3))
                                         return -ENOMEM;
                                 value[n_value++] = '\\';
                                 value[n_value++] = c;
                         }
 
+                        /* Escaped newlines (aka "continuation lines") are eaten up entirely */
                         break;
 
                 case COMMENT: