[comp.unix.wizards] Possible bug in sed

jlbrand@pttesac.UUCP (Jack Brand) (02/27/90)

sed(1) gurus,

I need help confirming whether I have found a bug in sed(1) or if
I am misinterpreting the manual.  It has to do with the 't' (test)
command.

Consider the following:

	sed -n "
	s/aaa/bbb/p
	n
	s/xxx/yyy/
	t success
	q
	: success

	[ rest of sed(1) commands ... ]

	"

Assume the first substitution succeeds.  A new line of input is
then read into the pattern space with the 'n' command.  Assume
the next substitution fails.  Now, according to the manual pages
for sed(1):

	t label   Test.  Branch to the : command bearing the label
		  if any substitutions have been made since the most
	   	  reading of an input line or execution of a 't'.  If
		  label is empty, branch to the end of the script.

This is not true.  The transfer to label "success" WILL take place,
even though no substitution has been made "since the most recent
reading of an input line."  sed(1) is "remembering" the successful
substitution of "bbb" for "aaa" earlier.

In the code it's easy to see why this is happening.  The integer
"sflag" gets set to 1 when a substitution happens.  This is what
gets checked by the 't' command to determine whether to transfer
to the label.  "sflag" gets set back to 0 after each 't' (true
to the manual) but NOT after the reading of an input line with
'n' or 'N'.  Adding the lines in to accomplish this seems to work;
is this what should be done?  Has anyone run into this?

For the record, a 'what /bin/sed' reveals:

/bin/sed:
	/bin/sed.sl 1.1 3.0 01/09/88 35043 iw

Thanks in advance for any light you can shed.

Jack Brand
...att!pacbell!pttesac!jlbrand
(415) 545-8332

zmact61@doc.ic.ac.uk (D Spinellis) (02/27/90)

In article <1567@pttesac.UUCP> jlbrand@pttesac.UUCP (Jack Brand) writes:
>
>sed(1) gurus,
>
>I need help confirming whether I have found a bug in sed(1) or if
>I am misinterpreting the manual.

I presume it is time for a rerun.

From: diomidis@ecrcvax.UUCP (Diomidis Spinellis)
Newsgroups: comp.bugs.4bsd
Subject: Re: `t' command of sed does not work fine.
Message-ID: <760@ecrcvax.UUCP>
Date: 23 Aug 89 10:44:54 GMT
References: <2427@srava.sra.JUNET>
Reply-To: diomidis@ecrcvax.UUCP (Diomidis Spinellis)
Organization: ECRC, Munich 81, West Germany
Lines: 75

In article <2427@srava.sra.JUNET> katsu@sra.JUNET (Katsuhiro Watanabe) writes:
>
>	Hello, world. I am using 4.3BSD on VAX.
> 	I found a bug on sed of BSDs, so I would like to report it.
>
>	Even if no substitution have been made before `t'(since
>the most recent reading of and input line), it often branches.
>
> [...]
>
>	Is this known yet?

I sent a bug report and fix to 4bsd-bugs@BERKELEY.EDU on May 18th 1989.
Here is the report and fix:

Subject: Sed does not clear the flag used by `t' when reading new input
Index: 	bin/sed 4.3BSD

Description:
	According to the sed manual page the test command `t'
	branches to the : command bearing the label specified
	if any substitutions have been made since the most recent
	reading of an input line or execution of a `t'.
	In the 4.3BSD version of sed the reading of an input line
	does not cancel the effect of substitutions made before
	reading the new line.  Thus if any of those substitutions
	succeded the first `t' to be executed will succeeed even if
	a new input line has been read.

Repeat-By:
	Execute the following:

sed -e '
s/hello/goodbye/
/goodbye/d
t n
a\
t failed
b
: n
a\
t succeeded' <<EOF
hello
world
EOF

	After reading the line containing `hello' the substitution will 
	succeed.  The d command starts a new cycle and thus the next t 
	should fail unless a substitution succeeds again.  After reading 
	`world' the substitution fails, but because of the bug, t succeeds 
	and the message `t succeeded' is printed.

Fix:
	Modify "bin/sed/sed1.c" as follows:

*** sed1.c.orig	Thu May 18 14:24:39 1989
--- sed1.c	Thu May 18 14:19:48 1989
***************
*** 650,655 ****
--- 650,656 ----
  {
  	register char	*p1, *p2;
  	register	c;
+ 	sflag = 0;
  	p1 = addr;
  	p2 = cbp;
  	for (;;) {

--
Diomidis Spinellis                  Internet:                 dds@cc.ic.ac.uk
Department of Computing             UUCP:                    ...!ukc!iccc!dds
Imperial College                    JANET:                    dds@uk.ac.ic.cc
London SW7 2BZ                      #include "/dev/tty"