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.

Full log


View this message in rfc822 format

From: help-debbugs <at> gnu.org (GNU bug Tracking System)
To: Eric Blake <eblake <at> redhat.com>
Cc: tracker <at> debbugs.gnu.org
Subject: bug#32494: closed (t incorrectly branching)
Date: Tue, 21 Aug 2018 15:53:02 +0000
[Message part 1 (text/plain, inline)]
Your message dated Tue, 21 Aug 2018 10:52:52 -0500
with message-id <3e98f187-b4b8-cbc3-a3c9-ef92a8dd56ed <at> redhat.com>
and subject line Re: bug#32494: t incorrectly branching
has caused the debbugs.gnu.org bug report #32494,
regarding t incorrectly branching
to be marked as done.

(If you believe you have received this mail in error, please contact
help-debbugs <at> gnu.org.)


-- 
32494: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=32494
GNU Bug Tracking System
Contact help-debbugs <at> gnu.org with problems
[Message part 2 (message/rfc822, inline)]
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 3 (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 4 (application/pgp-signature, inline)]
[Message part 5 (message/rfc822, inline)]
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


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.