bin/189827: sed treats some absolute addr2 of `N' command as relative

Hiroto Kagotani hiroto.kagotani at gmail.com
Thu May 15 05:30:00 UTC 2014


>Number:         189827
>Category:       bin
>Synopsis:       sed treats some absolute addr2 of `N' command as relative
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu May 15 05:30:00 UTC 2014
>Closed-Date:
>Last-Modified:
>Originator:     Hiroto Kagotani
>Release:        10.0-RELEASE-p2
>Organization:
>Environment:
FreeBSD myhost.mydomain 10.0-RELEASE-p2 FreeBSD 10.0-RELEASE-p2 #0: Tue Apr 29 17:06:01 UTC 2014     root at amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  amd64

>Description:
As sed's `N' command appends the next line of input to the pattern space,
absolute addr2 having odd number difference from addr1 does not
exactly match `linenum'.  In this case, after r192732, sed treats
addr2 as relative without checking the address type.  As the result,
`N' command with 2 addresses sometimes produces weird output.

I confirmed that sed in r168258 and r192731 works as expected.
sed before r168258 has another related `N' command bug,
which also exists in OpenBSD and NetBSD.

(discussed in https://forums.freebsd.org/viewtopic.php?t=37343)

>How-To-Repeat:
% seq 1 10 | sed '3,3N; s/\n/-/'
1
2
3-4
5
6
7
8
9
10

% seq 1 10 | sed '3,4N; s/\n/-/'
1
2
3-4
5-6  <-- unexpected
7-8  <-- unexpected
9
10

% seq 1 10 | sed '3,5N; s/\n/-/'
1
2
3-4
5-6
7
8
9
10

>Fix:
Attached is my simple fix.
Some complex testcases using `N' command should be added.

Patch attached with submission follows:

Index: usr.bin/sed/process.c
===================================================================
--- usr.bin/sed/process.c	(revision 266006)
+++ usr.bin/sed/process.c	(working copy)
@@ -292,7 +292,8 @@
 				cp->startline = 0;
 				lastaddr = 1;
 				r = 1;
-			} else if (linenum - cp->startline <= cp->a2->u.l)
+			} else if (cp->a2->type == AT_RELLINE &&
+				   linenum - cp->startline <= cp->a2->u.l)
 				r = 1;
 			else if ((cp->a2->type == AT_LINE &&
 				   linenum > cp->a2->u.l) ||


>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list