From nobody@FreeBSD.org  Wed Apr 19 16:05:48 2006
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 09F2516A40D
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 19 Apr 2006 16:05:48 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 71DF943D48
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 19 Apr 2006 16:05:46 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id k3JG5kmD028693
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 19 Apr 2006 16:05:46 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id k3JG5kdR028692;
	Wed, 19 Apr 2006 16:05:46 GMT
	(envelope-from nobody)
Message-Id: <200604191605.k3JG5kdR028692@www.freebsd.org>
Date: Wed, 19 Apr 2006 16:05:46 GMT
From: Andrey Zholos <aaz@althenia.net>
To: freebsd-gnats-submit@FreeBSD.org
Subject: sed "P" command broken on empty pattern space
X-Send-Pr-Version: www-2.3

>Number:         96052
>Category:       bin
>Synopsis:       [patch] sed(1) "P" command broken on empty pattern space
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Apr 19 16:10:15 GMT 2006
>Closed-Date:    Sat Jun 03 07:22:03 GMT 2006
>Last-Modified:  Sat Jun 03 07:22:03 GMT 2006
>Originator:     Andrey Zholos
>Release:        FreeBSD 6.1-PRERELEASE
>Organization:
>Environment:
FreeBSD sigma 6.1-PRERELEASE FreeBSD 6.1-PRERELEASE #5: Fri Mar 24 21:54:25 EET 2006     root@sigma:/usr/obj/usr/src/sys/SIGMA  i386

>Description:
If the "P" command is used once on a non-empty pattern space and then again on
an empty pattern space, the second usage restores the pattern space length to
the length that it had when the first "P" was used.


# echo 111 | sed -ne 'G;P;s/.*//;l;P;l'
111
$

\000\000\000\000$


Here, when the first "P" is used, there are 4 characters in the pattern space
("111\n"). The "s" clears the pattern space, but after the second "P" there are
4 characters there again.

>How-To-Repeat:
See above.
>Fix:
--- /usr/src/usr.bin/sed/process.c.orig	Sat Apr  9 17:31:41 2005
+++ /usr/src/usr.bin/sed/process.c	Wed Apr 19 17:54:18 2006
@@ -188,12 +188,11 @@
 			case 'P':
 				if (pd)
 					break;
-				if (psl != 0 &&
-				    (p = memchr(ps, '\n', psl)) != NULL) {
+				if ((p = memchr(ps, '\n', psl)) != NULL) {
 					oldpsl = psl;
 					psl = p - ps;
 				}
 				OUT(ps)
 				if (p != NULL)
 					psl = oldpsl;
 				break;


When psl == 0, p is not reset to NULL and oldpsl is not set, so "psl = oldpsl"
restores psl to whatever it was on a previous "P" command. So, one possible
fix is to drop the test for psl != 0.

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->patched 
State-Changed-By: krion 
State-Changed-When: Sun May 28 06:36:04 UTC 2006 
State-Changed-Why:  
Committed into HEAD. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=96052 
State-Changed-From-To: patched->closed 
State-Changed-By: krion 
State-Changed-When: Sat Jun 3 07:22:01 UTC 2006 
State-Changed-Why:  
Committed. Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=96052 
>Unformatted:
