diff --git a/usr.bin/sed/process.c b/usr.bin/sed/process.c index d06dc09542e..c177524a528 100644 --- a/usr.bin/sed/process.c +++ b/usr.bin/sed/process.c @@ -436,14 +436,12 @@ substitute(struct s_command *cp) * and at the end of the line, terminate. */ if (match[0].rm_so == match[0].rm_eo) { - if (*s == '\0' || *s == '\n') - slen = -1; - else - slen--; - if (*s != '\0') { + if (slen > 0) { cspace(&SS, s++, 1, APPEND); + slen--; le++; - } + } else + slen = -1; lastempty = 1; } else lastempty = 0; diff --git a/usr.bin/sed/tests/sed2_test.sh b/usr.bin/sed/tests/sed2_test.sh index f5061961256..0b849b1ea13 100755 --- a/usr.bin/sed/tests/sed2_test.sh +++ b/usr.bin/sed/tests/sed2_test.sh @@ -159,6 +159,19 @@ minus_e_body() atf_check -o 'inline:--\nab\n' sed $'1 i\\\n--' a } +atf_test_case command_D +command_D_head() +{ + atf_set "descr" "Test handling of an empty pattern space" +} +command_D_body() +{ + printf "hello\n\nworld\n" > a + + atf_check -o file:a sed -e 's/^//;P;D' a + atf_check -o file:a sed -e 's/^//;$!N;P;D' a +} + atf_init_test_cases() { atf_add_test_case inplace_command_q @@ -169,4 +182,5 @@ atf_init_test_cases() atf_add_test_case hex_subst atf_add_test_case bracket_y atf_add_test_case minus_e + atf_add_test_case command_D }