grep: avoid regression with -mN and any of -q, -l, -L

* src/grep.c (grepbuf): Handle this case: echo x|grep -l -m1 .
making it print only the file name, and not the matched line.
(main): Set out_quiet also when exit_on_match (-q) is set, so
"echo x|grep -q -m1 ." no longer prints the matched line.
* tests/max-count-overread: Add those tests, from
https://bugs.gnu.org/68989#21
This commit is contained in:
Jim Meyering 2025-04-07 22:21:16 -07:00
parent a4628e58dd
commit 52418599b3
2 changed files with 13 additions and 1 deletions

View File

@ -1487,6 +1487,8 @@ grepbuf (char *beg, char const *lim)
break;
if (!out_invert || p < b)
{
if (list_files != LISTFILES_NONE)
return 1;
char *prbeg = out_invert ? p : b;
char *prend = out_invert ? b : endp;
prtext (prbeg, prend);
@ -2899,7 +2901,7 @@ main (int argc, char **argv)
if (max_count == INTMAX_MAX)
done_on_match = true;
}
out_quiet = count_matches | done_on_match;
out_quiet = count_matches | done_on_match | exit_on_match;
if (out_after < 0)
out_after = default_context;

View File

@ -18,4 +18,14 @@ printf 'x\nx\nx\n' >in || framework_failure
(grep -m2 x >/dev/null && head -n1) <in >out || fail=1
compare exp out || fail=1
# The following two tests would fail before v3.11-70
echo x > in || framework_failure_
echo in > exp || framework_failure_
grep -l -m1 . in > out || fail=1
compare exp out || fail=1
# Ensure that this prints nothing and exits successfully.
grep -q -m1 . in > out || fail=1
compare /dev/null out || fail=1
Exit $fail