summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-08-23 02:39:47 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-08-23 02:39:47 +0200
commit3d8b96d58d74d775e34de8d19a03db5380eb8f2c (patch)
treec836d8788edcc5af94d8782857f70d9aca5b18a1
parentb276e418351612341e31da6385edbc3c91d2f6e8 (diff)
grep: fix -o match with empty string (suggested by Colin Watson <cjwatson@ubuntu.com>)
function old new delta grep_file 1216 1251 +35 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--findutils/grep.c10
-rwxr-xr-xtestsuite/grep.tests4
2 files changed, 11 insertions, 3 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index 688ea6ab2..024f27609 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -461,15 +461,19 @@ static int grep_file(FILE *file)
if (found)
print_line(gl->pattern, strlen(gl->pattern), linenum, ':');
} else while (1) {
+ unsigned start = gl->matched_range.rm_so;
unsigned end = gl->matched_range.rm_eo;
+ unsigned len = end - start;
char old = line[end];
line[end] = '\0';
- print_line(line + gl->matched_range.rm_so,
- end - gl->matched_range.rm_so,
- linenum, ':');
+ /* Empty match is not printed: try "echo test | grep -o ''" */
+ if (len != 0)
+ print_line(line + start, len, linenum, ':');
if (old == '\0')
break;
line[end] = old;
+ if (len == 0)
+ end++;
#if !ENABLE_EXTRA_COMPAT
if (regexec(&gl->compiled_regex, line + end,
1, &gl->matched_range, REG_NOTBOL) != 0)
diff --git a/testsuite/grep.tests b/testsuite/grep.tests
index 520a1840f..ffce033e6 100755
--- a/testsuite/grep.tests
+++ b/testsuite/grep.tests
@@ -98,5 +98,9 @@ testing "grep -o does not loop forever" \
'grep -o "[^/]*$"' \
"test\n" \
"" "/var/test\n"
+testing "grep -o does not loop forever on zero-length match" \
+ 'grep -o "" | head -n1' \
+ "" \
+ "" "test\n"
exit $FAILCOUNT