GNU bug report logs - #32494
t incorrectly branching

Previous Next

Package: sed;

Reported by: Ruben Maes <ruben <at> janmaes.com>

Date: Tue, 21 Aug 2018 15:23:01 UTC

Severity: normal

Tags: notabug

Done: Eric Blake <eblake <at> redhat.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 32494 in the body.
You can then email your comments to 32494 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-sed <at> gnu.org:
bug#32494; Package sed. (Tue, 21 Aug 2018 15:23:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Ruben Maes <ruben <at> janmaes.com>:
New bug report received and forwarded. Copy sent to bug-sed <at> gnu.org. (Tue, 21 Aug 2018 15:23:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Ruben Maes <ruben <at> janmaes.com>
To: Sed Bugs <bug-sed <at> gnu.org>
Subject: t incorrectly branching
Date: Tue, 21 Aug 2018 12:36:25 +0200
[Message part 1 (text/plain, inline)]
printf 'Hello\n' | sed '
	s/foobar//
	t end
	s/Hello/Goodbye/
	:end'

This works as expected, it prints Goodbye.


printf 'Hello\n' | sed '
	s/Hello/Hello to you/
	s/foobar//
	t end
	s/Hello/Goodbye/
	:end'

Since t should only look at whether the *last* substitution changed the pattern space, it is my understanding that this should print:
	Goodbye to you
But sed prints instead:
	Hello to you

If I got this right, that means there's a bug in sed – maybe resetting the "last substitution was successful" flag isn't done properly? Or am I misunderstanding something here after all?


Tried on these versions, both did exactly the same:

$ sed --version | head -n1
sed (GNU sed) 4.2.2
sed (GNU sed) 4.5
[Message part 2 (application/pgp-signature, inline)]

Added tag(s) notabug. Request was from Eric Blake <eblake <at> redhat.com> to control <at> debbugs.gnu.org. (Tue, 21 Aug 2018 15:53:02 GMT) Full text and rfc822 format available.

Reply sent to Eric Blake <eblake <at> redhat.com>:
You have taken responsibility. (Tue, 21 Aug 2018 15:53:02 GMT) Full text and rfc822 format available.

Notification sent to Ruben Maes <ruben <at> janmaes.com>:
bug acknowledged by developer. (Tue, 21 Aug 2018 15:53:02 GMT) Full text and rfc822 format available.

Message #12 received at 32494-done <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: Ruben Maes <ruben <at> janmaes.com>, 32494-done <at> debbugs.gnu.org
Subject: Re: bug#32494: t incorrectly branching
Date: Tue, 21 Aug 2018 10:52:52 -0500
tag 32494 notabug
thanks

On 08/21/2018 05:36 AM, Ruben Maes wrote:
> printf 'Hello\n' | sed '
> 	s/Hello/Hello to you/
> 	s/foobar//
> 	t end
> 	s/Hello/Goodbye/
> 	:end'
> 
> Since t should only look at whether the *last* substitution changed the pattern space,

That's not how POSIX describes it:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
"[2addr]t [label]
    Test. Branch to the : command verb bearing the label if any 
substitutions have been made since the most recent reading of an input 
line or execution of a t. If label is not specified, branch to the end 
of the script."

'info sed' words it a bit differently:

't LABEL'
     Branch to LABEL only if there has been a successful 's'ubstitution
     since the last input line was read or conditional branch was taken.
     The LABEL may be omitted, in which case the next cycle is started.

which seems to emphasize that the previous 't' must have been 
successfully taken before the condition gets reset (but if the last 
conditional 't' was not taken, then there has not been a successful 
match, so I don't know if the difference can be observed in practice).

> it is my understanding that this should print:
> 	Goodbye to you
> But sed prints instead:
> 	Hello to you

sed is behaving correctly; it is your understanding that was off.  It is 
not "branch if last substitution succeeded", but "branch if ANY 
substitution has succeeded since the last input or 't'".

One possible fix to your script, then, is to bound any substitution that 
you want to test in isolation with an earlier 't', perhaps looking 
something like:

t reset
: reset
s/...//
t end

such that whether or not 't reset' fires, execution resumes at s/// in 
question with the condition cleared.

As such, I'm closing this as not a bug, but feel free to add more 
comments on the topic.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org




Information forwarded to bug-sed <at> gnu.org:
bug#32494; Package sed. (Tue, 21 Aug 2018 16:01:02 GMT) Full text and rfc822 format available.

Message #15 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Ruben Maes <ruben <at> janmaes.com>
To: Sed Bugs <bug-sed <at> gnu.org>
Subject: Re: t incorrectly branching
Date: Tue, 21 Aug 2018 12:48:09 +0200
[Message part 1 (text/plain, inline)]
Okay, nevermind. There's just a grave error in the tutorial http://www.grymoire.com/Unix/Sed.html.

t isn't defined to look at the last substitution, but at all substitutions since the last input line was read and since the last t or T command.

Sorry for the noise.
[Message part 2 (application/pgp-signature, inline)]

Information forwarded to bug-sed <at> gnu.org:
bug#32494; Package sed. (Tue, 21 Aug 2018 16:01:04 GMT) Full text and rfc822 format available.

Message #18 received at 32494 <at> debbugs.gnu.org (full text, mbox):

From: Ruben Maes <ruben <at> janmaes.com>
To: 32494 <at> debbugs.gnu.org
Subject: Re: t incorrectly branching
Date: Tue, 21 Aug 2018 17:31:55 +0200
[Message part 1 (text/plain, inline)]
Okay, nevermind. There's just a grave error in the tutorial http://www.grymoire.com/Unix/Sed.html.

t isn't defined to look at the last substitution, but at all substitutions since the last input line was read and since the last t or T command.

Sorry for the noise.
[Message part 2 (application/pgp-signature, inline)]

Information forwarded to bug-sed <at> gnu.org:
bug#32494; Package sed. (Tue, 21 Aug 2018 16:11:02 GMT) Full text and rfc822 format available.

Message #21 received at 32494 <at> debbugs.gnu.org (full text, mbox):

From: Davide Brini <dave_br <at> gmx.com>
To: Ruben Maes <ruben <at> janmaes.com>
Cc: 32494 <at> debbugs.gnu.org
Subject: Re: bug#32494: t incorrectly branching
Date: Tue, 21 Aug 2018 18:09:58 +0200
On Tue, 21 Aug 2018 12:36:25 +0200, Ruben Maes <ruben <at> janmaes.com> wrote:

> printf 'Hello\n' | sed '
> 	s/foobar//
> 	t end
> 	s/Hello/Goodbye/
> 	:end'
> 
> This works as expected, it prints Goodbye.
> 
> 
> printf 'Hello\n' | sed '
> 	s/Hello/Hello to you/
> 	s/foobar//
> 	t end
> 	s/Hello/Goodbye/
> 	:end'
> 
> Since t should only look at whether the *last* substitution changed the
> pattern space, it is my understanding that this should print: Goodbye to
> you But sed prints instead:
> 	Hello to you
> 
> If I got this right, that means there's a bug in sed – maybe resetting
> the "last substitution was successful" flag isn't done properly? Or am I
> misunderstanding something here after all?

Here's what the man says:

"If a s/// has done a successful substitution since the last input line was
read and since the last t or T command, then branch to label"

The standard says:

"Branch to the : command verb bearing the label if any substitutions have
been made since the most recent reading of an input line or execution of a
t. If label is not specified, branch to the end of the script."

So it looks to me like sed is producing the expected behavior.

-- 
D.




Information forwarded to bug-sed <at> gnu.org:
bug#32494; Package sed. (Tue, 21 Aug 2018 16:56:02 GMT) Full text and rfc822 format available.

Message #24 received at 32494 <at> debbugs.gnu.org (full text, mbox):

From: Ruben Maes <ruben <at> janmaes.com>
To: 32494 <at> debbugs.gnu.org
Subject: Re: bug#32494: t incorrectly branching
Date: Tue, 21 Aug 2018 18:55:07 +0200
Might I remark that your bug tracking system is frustratingly slow. I noticed the mistake pretty soon after my report and sent a reply 12 minutes after reporting.

The initial report did not appear until almost an hour after I had sent it, and the follow-up message at least half an hour after that.




Information forwarded to bug-sed <at> gnu.org:
bug#32494; Package sed. (Tue, 21 Aug 2018 17:55:02 GMT) Full text and rfc822 format available.

Message #27 received at 32494 <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: Ruben Maes <ruben <at> janmaes.com>, 32494 <at> debbugs.gnu.org
Subject: Re: bug#32494: t incorrectly branching
Date: Tue, 21 Aug 2018 12:54:39 -0500
On 08/21/2018 11:55 AM, Ruben Maes wrote:
> Might I remark that your bug tracking system is frustratingly slow. I noticed the mistake pretty soon after my report and sent a reply 12 minutes after reporting.

It may also be compounded by the human moderator queue responsible for 
letting first-time poster's messages through.  While we tend to 
whitelist senders so that future messages don't need a human review, 
there's still a good chance that several initial messages from a new 
poster all pile up until someone actually has time to flush the queue.

> 
> The initial report did not appear until almost an hour after I had sent it, and the follow-up message at least half an hour after that.

Email is an interesting medium - it has great resiliency to temporary 
network outages, but at the expense of no real-time tracking and 
sometimes out-of-order delivery or or lengthy delays due to exponential 
back-off queuing choices at any number of hops along the way.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Wed, 19 Sep 2018 11:24:07 GMT) Full text and rfc822 format available.

This bug report was last modified 6 years and 277 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.