>Synopsis:      sed scripts specified with -e are missing a newline
>Category:      user
>Environment:
        System      : OpenBSD 5.3
        Details     : OpenBSD 5.3 (GENERIC.MP) #58: Tue Mar 12 18:43:53 MDT 2013
                         
[email protected]:/usr/src/sys/arch/i386/compile/GENERIC.MP

        Architecture: OpenBSD.i386
        Machine     : i386
>Description:
        The POSIX standard at
        
         http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
        
        states that for the sed utility,
        
         "commands specified by each -e or -f option shall be added to
         the script in the order specified. When each addition is made,
         if the previous addition (if any) was from a -e option, a
         <newline> shall be inserted before the new addition.  The
         resulting script shall have the same properties as the script
         operand..."
        
        OpenBSD's implementation of sed violates these requirements.

>How-To-Repeat:
        Note that when given the following command
        
         printf 'line1\nline2\n' | sed -n -e '1c\' -e 'sometext' -e '2p'
        
        OpenBSD's implementation of sed outputs the single line
        
         sometextline2
        
        It appears that sed forgets to insert a newline into the script
        after 'sometext'.  Indeed, if the same sed instructions are
        entered as a single script operand,
        
         printf 'line1\nline2\n' | sed -n '1c\
         sometext
         2p'
        
        then OpenBSD outputs the pair of lines
        
         sometext
         line2
        
        POSIX requires that OpenBSD produce the same output in both cases.

>Fix:
        The following patch inserts a newline character at the end of
        each string specified with the -e option.  (It also inserts a
        newline character at the end of the command-line script
        operand.)

--- usr.bin/sed/main.c.orig     Thu Oct 29 04:34:07 2009
+++ usr.bin/sed/main.c  Sun Jul 28 21:40:33 2013
@@ -232,6 +232,7 @@
                                        goto again;
                                } else {
                                        script = script->next;
+                                       *p++ = '\n';
                                        *p = '\0';
                                        linenum++;
                                        return (*outbuf);

Reply via email to