NetBSD Problem Report #45955

From www@NetBSD.org  Thu Feb  9 01:57:26 2012
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [149.20.53.66])
	by www.NetBSD.org (Postfix) with ESMTP id 96DD563D753
	for <gnats-bugs@gnats.NetBSD.org>; Thu,  9 Feb 2012 01:57:26 +0000 (UTC)
Message-Id: <20120209015725.C2B9F63BD87@www.NetBSD.org>
Date: Thu,  9 Feb 2012 01:57:25 +0000 (UTC)
From: zilla@kayari.org
Reply-To: zilla@kayari.org
To: gnats-bugs@NetBSD.org
Subject: sed(1) fails to append newline after 'c' command
X-Send-Pr-Version: www-1.0

>Number:         45955
>Notify-List:    uwe@netbsd.org
>Category:       bin
>Synopsis:       sed(1) fails to append newline after 'c' command
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Feb 09 02:00:00 +0000 2012
>Last-Modified:  Fri Feb 10 03:35:01 +0000 2012
>Originator:     Jonathan Wakely
>Release:        NetBSD 5.1
>Organization:
>Environment:
NetBSD gcc70.fsffrance.org 5.1 NetBSD 5.1 (GENERIC) #0: Sat Nov 6 13:19:33 UTC 2010 builds@b6.netbsd.org:/home/builds/ab/netbsd-5-1-RELEASE/amd64/201011061943Z-obj/home/builds/ab/netbsd-5-1-RELEASE/src/sys/arch/amd64/compile/GENERIC amd64
>Description:
in the command below sed should:

1) copy 'bar' (without following newline) to pattern space
2) apply 'c' command which:
2a) delete pattern space
2b) print 'bar'
3) print (empty) pattern space
4) append newline

step 4 is missing
>How-To-Repeat:
echo foo | sed 'c\
bar'

expected result is 'bar' followed by newline

actual result is just 'bar'
>Fix:
Explicitly include a newline in the replacement text:

echo foo | sed 'c\
bar\
'

but this produces two newlines on other platforms (e.g. with GNU or Solaris sed)

>Release-Note:

>Audit-Trail:
From: "Valeriy E. Ushakov" <uwe@stderr.spb.ru>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: bin/45955: sed(1) fails to append newline after 'c' command
Date: Thu, 9 Feb 2012 16:08:56 +0400

 My sed is very rusty, but I think you got the sequence of events
 slighly wrong.  I would consider "c" to be more like a shorthand for
 "i" + "d", so 3) and 4) in your list never happen (like for "d").

 The question is what happens at 2b) - and here the described effect
 only manifests when the script is specified with -e or if the c
 command is last in the file (-f) and the file has no final newline.

 "a" and "i" behave the same as "c" here, btw.


 Compare

   $ cat pr45955.sed # without newline at the end of the file
   c\
   bar$ echo foo | sed -f pr45955.sed
   bar$

 vs.

   $ cat pr45955.sed
   c\
   bar
   $ echo foo | sed -f pr45955.sed
   bar
   $

 For GNU sed output is "bar\n" for both scripts.

 Of course, if "c" is not the last command in the file there's simply
 no way to specify "text" without the final newline, so any knee jerk
 objections (and, yes, it was mine too :) "how do you replace without a
 newline?" are overruled.

 So the observed effect is here because when a command line (-e) or the
 last line of a script (-f) are processed and there's no final newline
 and the line contains "text" for "a", "c" or "i" command, the newline
 is not added to the "text".

 I think it's a bug, but a bug in processing of script's source, not in
 "c" command.

 -uwe

From: "Valeriy E. Ushakov" <uwe@stderr.spb.ru>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: bin/45955: sed(1) fails to append newline after 'c' command
Date: Thu, 9 Feb 2012 17:19:10 +0400

 On Thu, Feb 09, 2012 at 16:08:56 +0400, Valeriy E. Ushakov wrote:

 > I think it's a bug, but a bug in processing of script's source, not in
 > "c" command.

 FWIW, I checked V7, and the implicit newline in -e is there, i.e. it
 outputs "bar\n" and "bar\n\n" for the original command and the
 workaround with explicit newline in -e respectively, like GNU sed.

 -uwe

From: "Valeriy E. Ushakov" <uwe@stderr.spb.ru>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: bin/45955: sed(1) fails to append newline after 'c' command
Date: Thu, 9 Feb 2012 17:31:09 +0400

 On Thu, Feb 09, 2012 at 17:19:10 +0400, Valeriy E. Ushakov wrote:

 > FWIW, I checked V7 [...]

 Heh, we actually do have a bug in "c" but a different one :)

 V7 sed(1) manual explicitely states that "c" starts the next cycle,
 like "d".

 So a a script that does

   c\
   c1
   c\
   c2

 should emit only "c1", as it does for v7 or gnu sed.

 Our sed emits both c1 and c2.

 -uwe

From: "Valeriy E. Ushakov" <uwe@stderr.spb.ru>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: bin/45955: sed(1) fails to append newline after 'c' command
Date: Fri, 10 Feb 2012 07:34:51 +0400

 On Thu, Feb 09, 2012 at 16:08:56 +0400, Valeriy E. Ushakov wrote:

 > So the observed effect is here because when a command line (-e) or the
 > last line of a script (-f) are processed and there's no final newline
 > and the line contains "text" for "a", "c" or "i" command, the newline
 > is not added to the "text".

 Forgot to mention, correct workaround is

 echo foo | sed 'c\
 bar
 '

 without the backslash (that just triggers the same bug).

 -uwe

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.