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 <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jun 3, 2009 at 18:19 UTC
84201eae771fbb1d774cf1356d89016053e56778
1 file changed
+5
-1
grep.c
+5
-1
@@ -331,7 +331,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
331
332
if (hit && p->word_regexp) {
333
if ((pmatch[0].rm_so < 0) ||
334
- (eol - bol) <= pmatch[0].rm_so ||
334
+ (eol - bol) < pmatch[0].rm_so ||
335
(pmatch[0].rm_eo < 0) ||
336
(eol - bol) < pmatch[0].rm_eo)
337
die("regexp returned nonsense");
@@ -350,6 +350,10 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
350
else
351
hit = 0;
352
353
+ /* Words consist of at least one character. */
354
+ if (pmatch->rm_so == pmatch->rm_eo)
355
+ hit = 0;
356
+
357
if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
358
/* There could be more than one match on the
359
* line, and the first match might not be