Bug 25585

Summary: sed.test 8.16 puts bugged sed into infinite loop
Product: Base System Reporter: wbriscoe <wbriscoe>
Component: miscAssignee: Diomidis Spinellis <dds>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 1.0-RELEASE   
Hardware: Any   
OS: Any   

Description wbriscoe 2001-03-07 10:10:00 UTC
Test 8.16 in sed.test tests the ability of a sed to handle an empty
regular expression as the first argument to a substitute command. If
used to test a sed which (erroneously) evaluates this at translation
time rather than at execution time, the bugged sed is put into an
infinite loop. This mode of failure seems excessive. Such a failing
sed is the Free Software Foundation's sed 3.02.
I found this problem when I needed a test suite for the simtelnet MSDOS
port of sed. I was advised by Diomidis Spinellis - the copyright holder
of sed.test - that he no longer maintains it. The bug also exists in
OpenBsd and NetBSD. Please forward this pr or advise how I may submit
to those ports. http://www.freebsd.org/send-pr.html contains
"Note: copy/paste will destroy TABs and spacing, and this web form should not be used to submit code as plain text". It does not say what
should be used for that purpose. I hope the fix below is not mangled.
Should it be so, I hope somebody will come back to me to suggest
an improvement.

Fix: The following solves the problem by limiting the number of iterations
of the infinite loop. There are probably more elegant ways of achieving
the same functionality.

09:43:26 05 /u/t150drs/freebsd: diff -C 3 sed.test.1.2 sed.test.new


--- 504,521 ----
        if [ $BSD -eq 1 ] ; then
                echo 'BSD sed does not handle branch defined REs'
        else
+ #               The code below enters an infinite loop when testing
+ #               a sed which (wrongly) determines the meaning of the
+ #               empty RE in s//Y/p at compile time rather than run
+ #               time. w.briscoe@ponl.com 2001-03-07
+ #             echo 'eeefff' | $SED -e 'p' -e 's/e/X/p' -e ':x' \
+ #                 -e 's//Y/p' -e '/f/bx'
                echo 'eeefff' | $SED -e 'p' -e 's/e/X/p' -e ':x' \
!                   -e 's//Y/p' \
!                     -e x \
!                     -e /.\{10\}/{s/.*/ERROR/ -e b -e } -e s/.*/&./ \
!                     -e x \
!                     -e '/f/bx'
        fi
  }

09:43:29 05 /u/t150drs/freebsd:--DZnfA53rqDgOGQsafEKdPqroURiIf0zhd3PHX1A10WiRgRfU
Content-Type: text/plain; name="file.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="file.diff"

*** sed.test.1.2        Wed Mar  7 09:27:05 2001
--- sed.test.new        Wed Mar  7 09:44:22 2001
***************
*** 504,511 ****
        if [ $BSD -eq 1 ] ; then
                echo 'BSD sed does not handle branch defined REs'
        else
                echo 'eeefff' | $SED -e 'p' -e 's/e/X/p' -e ':x' \
!                   -e 's//Y/p' -e '/f/bx'
        fi
  }
How-To-Repeat: Find a bugged sed - such as sed 3.02
Find the latest version (1.2) of sed.test from
http://www.freebsd.org/cgi/cvsweb.cg1/src/usr.bin/sed/TEST/sed.test
Run sed.test
test 8.16 does not complete.
Comment 1 Jens Schweikhardt freebsd_committer freebsd_triage 2003-07-19 18:58:21 UTC
Responsible Changed
From-To: freebsd-bugs->dds

Diomidis, can you have a look at this?
Comment 2 Diomidis Spinellis freebsd_committer freebsd_triage 2003-07-22 20:24:33 UTC
State Changed
From-To: open->patched

Implemented the suggested change in a bit more readable manner. 
Change verified against a problematic FSF GNU sed and the (now working) 
FreeBSD sed.
Comment 3 Diomidis Spinellis freebsd_committer freebsd_triage 2003-08-02 08:18:27 UTC
State Changed
From-To: patched->closed

Fix MFCed to RELENG_4