ash: expand: Fix trailing newlines processing in backquote expanding
authorDenys Vlasenko <vda.linux@googlemail.com>
Mon, 17 Feb 2020 09:24:32 +0000 (10:24 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Mon, 17 Feb 2020 09:24:32 +0000 (10:24 +0100)
Upstream commit:

    Date: Mon, 29 Apr 2019 19:13:37 +0500
    expand: Fix trailing newlines processing in backquote expanding

    According to POSIX.1-2008 we should remove newlines only at the end of
    the substitution. Newlines-only substitions causes dash to remove
    newlines before beggining of the substitution. The following code:

        cat <<END
        1
        $(echo "")
        2
        END

    prints "1<newline>2" instead of expected "1<newline><newline>2".

    This patch fixes trailing newlines processing in backquote expanding.

Signed-off-by: Nikolai Merinov <n.merinov@inango-systems.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/ash.c
shell/ash_test/ash-psubst/tick_in_heredoc.right [new file with mode: 0644]
shell/ash_test/ash-psubst/tick_in_heredoc.tests [new file with mode: 0755]
shell/hush_test/hush-psubst/tick_in_heredoc.right [new file with mode: 0644]
shell/hush_test/hush-psubst/tick_in_heredoc.tests [new file with mode: 0755]

index 90496ba7e84b50f84ff16acd6c9bf98c12e91f88..dfe6d1c485692c434cebb60d2dae4baef16810ce 100644 (file)
@@ -6578,7 +6578,7 @@ expbackq(union node *cmd, int flag)
 
        /* Eat all trailing newlines */
        dest = expdest;
-       for (; dest > (char *)stackblock() && dest[-1] == '\n';)
+       for (; dest > ((char *)stackblock() + startloc) && dest[-1] == '\n';)
                STUNPUTC(dest);
        expdest = dest;
 
diff --git a/shell/ash_test/ash-psubst/tick_in_heredoc.right b/shell/ash_test/ash-psubst/tick_in_heredoc.right
new file mode 100644 (file)
index 0000000..7e7bac6
--- /dev/null
@@ -0,0 +1,5 @@
+1
+
+2
+
+3
diff --git a/shell/ash_test/ash-psubst/tick_in_heredoc.tests b/shell/ash_test/ash-psubst/tick_in_heredoc.tests
new file mode 100755 (executable)
index 0000000..c8eb8f4
--- /dev/null
@@ -0,0 +1,7 @@
+cat <<END
+1
+$(echo "")
+2
+`echo ""`
+3
+END
diff --git a/shell/hush_test/hush-psubst/tick_in_heredoc.right b/shell/hush_test/hush-psubst/tick_in_heredoc.right
new file mode 100644 (file)
index 0000000..7e7bac6
--- /dev/null
@@ -0,0 +1,5 @@
+1
+
+2
+
+3
diff --git a/shell/hush_test/hush-psubst/tick_in_heredoc.tests b/shell/hush_test/hush-psubst/tick_in_heredoc.tests
new file mode 100755 (executable)
index 0000000..c8eb8f4
--- /dev/null
@@ -0,0 +1,7 @@
+cat <<END
+1
+$(echo "")
+2
+`echo ""`
+3
+END