Skip to content

Commit 84201ea

Browse files
René Scharfegitster
authored andcommitted
grep: fix empty word-regexp matches
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]>
1 parent a9b2d42 commit 84201ea

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

grep.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
331331

332332
if (hit && p->word_regexp) {
333333
if ((pmatch[0].rm_so < 0) ||
334-
(eol - bol) <= pmatch[0].rm_so ||
334+
(eol - bol) < pmatch[0].rm_so ||
335335
(pmatch[0].rm_eo < 0) ||
336336
(eol - bol) < pmatch[0].rm_eo)
337337
die("regexp returned nonsense");
@@ -350,6 +350,10 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
350350
else
351351
hit = 0;
352352

353+
/* Words consist of at least one character. */
354+
if (pmatch->rm_so == pmatch->rm_eo)
355+
hit = 0;
356+
353357
if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
354358
/* There could be more than one match on the
355359
* line, and the first match might not be

0 commit comments

Comments
 (0)