From nobody@FreeBSD.org  Thu May 15 05:20:08 2014
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by hub.freebsd.org (Postfix) with ESMTPS id DB4DDD31
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 15 May 2014 05:20:08 +0000 (UTC)
Received: from cgiserv.freebsd.org (cgiserv.freebsd.org [IPv6:2001:1900:2254:206a::50:4])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(Client did not present a certificate)
	by mx1.freebsd.org (Postfix) with ESMTPS id C967721DA
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 15 May 2014 05:20:08 +0000 (UTC)
Received: from cgiserv.freebsd.org ([127.0.1.6])
	by cgiserv.freebsd.org (8.14.8/8.14.8) with ESMTP id s4F5K8TO098950
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 15 May 2014 05:20:08 GMT
	(envelope-from nobody@cgiserv.freebsd.org)
Received: (from nobody@localhost)
	by cgiserv.freebsd.org (8.14.8/8.14.8/Submit) id s4F5K88p098947;
	Thu, 15 May 2014 05:20:08 GMT
	(envelope-from nobody)
Message-Id: <201405150520.s4F5K88p098947@cgiserv.freebsd.org>
Date: Thu, 15 May 2014 05:20:08 GMT
From: Hiroto Kagotani <hiroto.kagotani@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: sed treats some absolute addr2 of `N' command as relative
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         189827
>Category:       bin
>Synopsis:       [patch] sed(1) 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:  Thu May 15 07:23:04 UTC 2014
>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@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:
