diff mbox

[BUG] regression in builtin echo

Message ID 20160902131439.GA12162@gondor.apana.org.au (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show

Commit Message

Herbert Xu Sept. 2, 2016, 1:14 p.m. UTC
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>

Comments

Jilles Tjoelker Sept. 2, 2016, 2:16 p.m. UTC | #1
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.
Herbert Xu Sept. 2, 2016, 2:19 p.m. UTC | #2
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,
Jilles Tjoelker Sept. 2, 2016, 3:24 p.m. UTC | #3
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.
diff mbox

Patch

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