Skip to content

Commit 91af1b8

Browse files
mohd-akrammarkjdb
authored andcommitted
sed: Fix handling of an empty pattern space
Add a regression test. PR: 271791 Obtained from: OpenBSD (1.38 millert) MFC after: 2 weeks (cherry picked from commit 5982237)
1 parent 40c01de commit 91af1b8

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

usr.bin/sed/process.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,12 @@ substitute(struct s_command *cp)
439439
* and at the end of the line, terminate.
440440
*/
441441
if (match[0].rm_so == match[0].rm_eo) {
442-
if (*s == '\0' || *s == '\n')
443-
slen = -1;
444-
else
445-
slen--;
446-
if (*s != '\0') {
442+
if (slen > 0) {
447443
cspace(&SS, s++, 1, APPEND);
444+
slen--;
448445
le++;
449-
}
446+
} else
447+
slen = -1;
450448
lastempty = 1;
451449
} else
452450
lastempty = 0;

usr.bin/sed/tests/sed2_test.sh

+14
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ minus_e_body()
159159
atf_check -o 'inline:--\nab\n' sed $'1 i\\\n--' a
160160
}
161161

162+
atf_test_case command_D
163+
command_D_head()
164+
{
165+
atf_set "descr" "Test handling of an empty pattern space"
166+
}
167+
command_D_body()
168+
{
169+
printf "hello\n\nworld\n" > a
170+
171+
atf_check -o file:a sed -e 's/^//;P;D' a
172+
atf_check -o file:a sed -e 's/^//;$!N;P;D' a
173+
}
174+
162175
atf_init_test_cases()
163176
{
164177
atf_add_test_case inplace_command_q
@@ -169,4 +182,5 @@ atf_init_test_cases()
169182
atf_add_test_case hex_subst
170183
atf_add_test_case bracket_y
171184
atf_add_test_case minus_e
185+
atf_add_test_case command_D
172186
}

0 commit comments

Comments
 (0)