bsdgrep: fix zero-length matches without the -o flag

r316477 broke zero-length matches when not using the -o flag, by
skipping over them entirely.

Add a regression test so that it doesn't break again in the future.

Submitted by:	Kyle Evans <kevans91 at ksu.edu>
Reviewed by:	cem emaste ngie
Differential Revision:	https://reviews.freebsd.org/D10333
This commit is contained in:
Ed Maste 2017-04-17 14:59:55 +00:00
parent 22130a21ba
commit e06ffa3230
2 changed files with 18 additions and 3 deletions

View File

@ -377,6 +377,20 @@ egrep_empty_invalid_body()
{
atf_check -s exit:1 egrep '{' /dev/null
}
atf_test_case zerolen
zerolen_head()
{
atf_set "descr" "Check for successful zero-length matches with ^$"
}
zerolen_body()
{
printf "Eggs\n\nCheese" > test1
atf_check -o inline:"\n" grep -e "^$" test1
atf_check -o inline:"Eggs\nCheese\n" grep -v -e "^$" test1
}
# End FreeBSD
atf_init_test_cases()
@ -404,5 +418,6 @@ atf_init_test_cases()
atf_add_test_case f_file_empty
atf_add_test_case escmap
atf_add_test_case egrep_empty_invalid
atf_add_test_case zerolen
# End FreeBSD
}

View File

@ -352,9 +352,6 @@ procline(struct str *l, int nottext)
if (r == 0) {
lastmatches++;
lastmatch = pmatch;
/* Skip over zero-length matches */
if (pmatch.rm_so == pmatch.rm_eo)
continue;
if (m == 0)
c++;
@ -532,6 +529,9 @@ printline(struct str *line, int sep, regmatch_t *matches, int m)
/* --color and -o */
if ((oflag || color) && m > 0) {
for (i = 0; i < m; i++) {
/* Don't output zero length matches */
if (matches[i].rm_so == matches[i].rm_eo)
continue;
if (!oflag)
fwrite(line->dat + a, matches[i].rm_so - a, 1,
stdout);