On Thu, May 29, 2014 at 10:45 PM, Marc Aldorasi <[email protected]> wrote:
> With grep 2.18, the -m option would cause grep to stop reading input
> after printing the requested number of matching lines.  With version
> 2.19, grep reads the entire input before exiting.  Interestingly, grep
> does not read the entire input if the -c or -C0 options are added in
> addition to -m, and also when using -l or -q instead of -m.  I believe
> this is caused by commit 5122195.

Thanks a lot for the report.  Just in time.
I confirm that it's a bug introduced in 2.19.
To test, run "seq 1000000 > million", then
 "strace -e read grep 0 million" first using grep-2.18
(shows just a few read syscalls), and then with 2.19,
which shows grep reading the entire million-line file.

Here's an incomplete patch.  Obviously there's a lot more
to be added, including NEWS and a nontrivial test. This
was introduced by commit v2.18-140-g6f07900
diff --git a/src/grep.c b/src/grep.c
index acc08c7..7c0f8a8 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1043,7 +1043,7 @@ prtext (char const *beg, char const *lim)
     }
 
   after_last_match = bufoffset - (buflim - p);
-  pending = out_quiet ? 0 : out_after;
+  pending = out_quiet ? 0 : MAX (0, out_after);
   used = true;
   outleft -= n;
 }

Reply via email to