Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 17 Apr 2017 14:59:55 +0000 (UTC)
From:      Ed Maste <emaste@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r317052 - in head: contrib/netbsd-tests/usr.bin/grep usr.bin/grep
Message-ID:  <201704171459.v3HExtnZ054217@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: emaste
Date: Mon Apr 17 14:59:55 2017
New Revision: 317052
URL: https://svnweb.freebsd.org/changeset/base/317052

Log:
  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

Modified:
  head/contrib/netbsd-tests/usr.bin/grep/t_grep.sh
  head/usr.bin/grep/util.c

Modified: head/contrib/netbsd-tests/usr.bin/grep/t_grep.sh
==============================================================================
--- head/contrib/netbsd-tests/usr.bin/grep/t_grep.sh	Mon Apr 17 13:36:30 2017	(r317051)
+++ head/contrib/netbsd-tests/usr.bin/grep/t_grep.sh	Mon Apr 17 14:59:55 2017	(r317052)
@@ -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
 }

Modified: head/usr.bin/grep/util.c
==============================================================================
--- head/usr.bin/grep/util.c	Mon Apr 17 13:36:30 2017	(r317051)
+++ head/usr.bin/grep/util.c	Mon Apr 17 14:59:55 2017	(r317052)
@@ -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, reg
 	/* --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);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201704171459.v3HExtnZ054217>