git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] grep: fix empty word-regexp matches

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Jun 3, 2009, 16:19 UTC
Message-ID
<4A26A275.7000900@lsrfire.ath.cx>

The command "git grep -w ''" dies as soon as it encounters an empty line, reporting (wrongly) that "regexp returned nonsense". The first hunk of this patch relaxes the sanity check that is responsible for that, allowing matches to start at the end.

The second hunk complements it by making sure that empty matches are rejected if -w was specified, as they are not really words.

GNU grep does the same:
	$ echo foo | grep -c ''
	1
	$ echo foo | grep -c -w ''
	0
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 grep.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/grep.c b/grep.c
index 7bf4a60..92a47c7 100644
--- a/grep.c
+++ b/grep.c
@@ -331,7 +331,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
 
 	if (hit && p->word_regexp) {
 		if ((pmatch[0].rm_so < 0) ||
-		    (eol - bol) <= pmatch[0].rm_so ||
+		    (eol - bol) < pmatch[0].rm_so ||
 		    (pmatch[0].rm_eo < 0) ||
 		    (eol - bol) < pmatch[0].rm_eo)
 			die("regexp returned nonsense");
@@ -350,6 +350,10 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
 		else
 			hit = 0;
 
+		/* Words consist of at least one character. */
+		if (pmatch->rm_so == pmatch->rm_eo)
+			hit = 0;
+
 		if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
 			/* There could be more than one match on the
 			 * line, and the first match might not be
-- 
1.6.3.1
Message 1 of 1 in “grep: fix empty word-regexp matches”
  1. grep: fix empty word-regexp matchesRené Scharfe, Jun 3, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.