Skip to content

Commit

Permalink
grep: fix empty word-regexp matches
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
René Scharfe authored and gitster committed Jun 3, 2009
1 parent a9b2d42 commit 84201ea
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand Down

0 comments on commit 84201ea

Please sign in to comment.