dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [BUG] regression in builtin echo
       [not found] <CAKkO-EgCkv48unwXBjN1JdjSNx3_D1yCZQhde7WJedOFRUDaGg@mail.gmail.com>
@ 2016-09-01 20:18 ` Harald van Dijk
  2016-09-02 13:14   ` Herbert Xu
  0 siblings, 1 reply; 5+ messages in thread
From: Harald van Dijk @ 2016-09-01 20:18 UTC (permalink / raw)
  To: Luigi Tarenga, dash

[-- Attachment #1: Type: text/plain, Size: 783 bytes --]

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

[-- Attachment #2: echo.patch --]
[-- Type: text/x-patch, Size: 632 bytes --]

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;
 }

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] regression in builtin echo
  2016-09-01 20:18 ` [BUG] regression in builtin echo Harald van Dijk
@ 2016-09-02 13:14   ` Herbert Xu
  2016-09-02 14:16     ` Jilles Tjoelker
  0 siblings, 1 reply; 5+ messages in thread
From: Herbert Xu @ 2016-09-02 13:14 UTC (permalink / raw)
  To: Harald van Dijk; +Cc: luigi.tarenga, dash

Harald van Dijk <harald@gigawatt.nl> wrote:
> 
> While the original code implementing the echo command was overly 
> complicated, the simplified version does not do the right thing, as you 
> noticed.

Indeed.

However, we don't need to rewrite the function to fix this.

---8<---
Subject: builtin: Fix echo -n early termination

The commit 7a784244625d5489c0fc779201c349555dc5f8bc ("[BUILTIN]
Simplify echo command") broke echo -n by making it always terminate
after printing the first argument.

This patch fixes this by only terminating when we have reached
the end of the arguments.

Fixes: 7a784244625d ("[BUILTIN] Simplify echo command")
Reported-by: Luigi Tarenga <luigi.tarenga@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>


diff --git a/src/bltin/printf.c b/src/bltin/printf.c
index 1112253..a626cee 100644
--- a/src/bltin/printf.c
+++ b/src/bltin/printf.c
@@ -459,7 +459,7 @@ echocmd(int argc, char **argv)
 
 		if (likely(*argv))
 			nonl += print_escape_str("%s", NULL, NULL, *argv++);
-		if (nonl > 0)
+		if (likely((nonl + !*argv) > 1))
 			break;
 
 		c = *argv ? ' ' : '\n';

-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] regression in builtin echo
  2016-09-02 13:14   ` Herbert Xu
@ 2016-09-02 14:16     ` Jilles Tjoelker
  2016-09-02 14:19       ` Herbert Xu
  0 siblings, 1 reply; 5+ messages in thread
From: Jilles Tjoelker @ 2016-09-02 14:16 UTC (permalink / raw)
  To: Herbert Xu; +Cc: Harald van Dijk, luigi.tarenga, dash

On Fri, Sep 02, 2016 at 09:14:39PM +0800, Herbert Xu wrote:
> Harald van Dijk <harald@gigawatt.nl> wrote:

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

> Indeed.

> However, we don't need to rewrite the function to fix this.

> ---8<---
> Subject: builtin: Fix echo -n early termination

> The commit 7a784244625d5489c0fc779201c349555dc5f8bc ("[BUILTIN]
> Simplify echo command") broke echo -n by making it always terminate
> after printing the first argument.

> This patch fixes this by only terminating when we have reached
> the end of the arguments.

> Fixes: 7a784244625d ("[BUILTIN] Simplify echo command")
> Reported-by: Luigi Tarenga <luigi.tarenga@gmail.com>
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

> diff --git a/src/bltin/printf.c b/src/bltin/printf.c
> index 1112253..a626cee 100644
> --- a/src/bltin/printf.c
> +++ b/src/bltin/printf.c
> @@ -459,7 +459,7 @@ echocmd(int argc, char **argv)
>  
>  		if (likely(*argv))
>  			nonl += print_escape_str("%s", NULL, NULL, *argv++);
> -		if (nonl > 0)
> +		if (likely((nonl + !*argv) > 1))
>  			break;
>  
>  		c = *argv ? ' ' : '\n';

Unlike Harald van Dijk's patch, the above patch breaks \c. Per POSIX
(XSI option), \c shall cause all characters following it in the
arguments to be ignored (so not only in the argument where \c occurs).
For example:
  echo 'a\cb' c; echo d
shall write "ad" followed by a newline.

-- 
Jilles Tjoelker

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] regression in builtin echo
  2016-09-02 14:16     ` Jilles Tjoelker
@ 2016-09-02 14:19       ` Herbert Xu
  2016-09-02 15:24         ` Jilles Tjoelker
  0 siblings, 1 reply; 5+ messages in thread
From: Herbert Xu @ 2016-09-02 14:19 UTC (permalink / raw)
  To: Jilles Tjoelker; +Cc: Harald van Dijk, luigi.tarenga, dash

On Fri, Sep 02, 2016 at 04:16:31PM +0200, Jilles Tjoelker wrote:
>
> Unlike Harald van Dijk's patch, the above patch breaks \c. Per POSIX
> (XSI option), \c shall cause all characters following it in the
> arguments to be ignored (so not only in the argument where \c occurs).
> For example:
>   echo 'a\cb' c; echo d
> shall write "ad" followed by a newline.

Works for me:

$ build/src/dash -c "echo 'a\cb' c; echo d"
ad
$ 

AFAICS my patch doesn't change \c handling at all.  When we hit
\c print_escape_str will return 0x100, which guarantees that we
hit the berak.

Thanks,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] regression in builtin echo
  2016-09-02 14:19       ` Herbert Xu
@ 2016-09-02 15:24         ` Jilles Tjoelker
  0 siblings, 0 replies; 5+ messages in thread
From: Jilles Tjoelker @ 2016-09-02 15:24 UTC (permalink / raw)
  To: Herbert Xu; +Cc: Harald van Dijk, luigi.tarenga, dash

On Fri, Sep 02, 2016 at 10:19:12PM +0800, Herbert Xu wrote:
> On Fri, Sep 02, 2016 at 04:16:31PM +0200, Jilles Tjoelker wrote:

> > Unlike Harald van Dijk's patch, the above patch breaks \c. Per POSIX
> > (XSI option), \c shall cause all characters following it in the
> > arguments to be ignored (so not only in the argument where \c occurs).
> > For example:
> >   echo 'a\cb' c; echo d
> > shall write "ad" followed by a newline.

> Works for me:

> $ build/src/dash -c "echo 'a\cb' c; echo d"
> ad
> $ 

> AFAICS my patch doesn't change \c handling at all.  When we hit
> \c print_escape_str will return 0x100, which guarantees that we
> hit the berak.

You are right. The code is very tricky and my analysis without running
the code was wrong.

I think developing a shell is hard enough already without making the
code so tricky, though.

-- 
Jilles Tjoelker

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-09-02 15:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CAKkO-EgCkv48unwXBjN1JdjSNx3_D1yCZQhde7WJedOFRUDaGg@mail.gmail.com>
2016-09-01 20:18 ` [BUG] regression in builtin echo Harald van Dijk
2016-09-02 13:14   ` Herbert Xu
2016-09-02 14:16     ` Jilles Tjoelker
2016-09-02 14:19       ` Herbert Xu
2016-09-02 15:24         ` Jilles Tjoelker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).