diff mbox

[BUG] regression in builtin echo

Message ID 3efcd42c-e20b-3506-3d62-69b85c027ef4@gigawatt.nl (mailing list archive)
State Superseded
Delegated to: Herbert Xu
Headers show

Commit Message

Harald van Dijk Sept. 1, 2016, 8:18 p.m. UTC
On 01/09/16 10:13, Luigi Tarenga wrote:
> hi,
> I just find a problem in the dash distributed with arch linux.
> I didn't tested from dash source but the bug can be easily
> checked:
>
> dash 0.5.9-1
>
> $ echo one two three
> one two three
> $
> $ echo -n one two three
> one$

Yikes.

> with -n option it stop after printing one more parameter...
> do you have the same problem or it's caused by a patch in arch?

That's caused by <http://www.spinics.net/lists/dash/msg00942.html>, not 
by Arch. It's scary that this has gone totally unnoticed for more than a 
year.

While the original code implementing the echo command was overly 
complicated, the simplified version does not do the right thing, as you 
noticed.

Here's another attempt at a simplified implementation.

> Luigi
diff mbox

Patch

diff --git a/src/bltin/printf.c b/src/bltin/printf.c
index 9673e10..b7b6d68 100644
--- a/src/bltin/printf.c
+++ b/src/bltin/printf.c
@@ -447,16 +447,20 @@  echocmd(int argc, char **argv)
 	nonl = *++argv ? equal(*argv, "-n") : 0;
 	argv += nonl;
 
-	do {
-		int c;
+	if (*argv) {
+		for (;;) {
+			if (print_escape_str("%s", NULL, NULL, *argv))
+				return 0;
+
+			if (!*++argv)
+				break;
+
+			out1c(' ');
+		}
+	}
 
-		if (likely(*argv))
-			nonl += print_escape_str("%s", NULL, NULL, *argv++);
-		if (nonl > 0)
-			break;
+	if (!nonl)
+		out1c('\n');
 
-		c = *argv ? ' ' : '\n';
-		out1c(c);
-	} while (*argv);
 	return 0;
 }