From 52418599b30c6a42358599b65e2cbb7da2926b8e Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Mon, 7 Apr 2025 22:21:16 -0700 Subject: [PATCH] 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 --- src/grep.c | 4 +++- tests/max-count-overread | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/grep.c b/src/grep.c index 192f9d1..f657607 100644 --- a/src/grep.c +++ b/src/grep.c @@ -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; diff --git a/tests/max-count-overread b/tests/max-count-overread index f829cc5..e15ea77 100755 --- a/tests/max-count-overread +++ b/tests/max-count-overread @@ -18,4 +18,14 @@ printf 'x\nx\nx\n' >in || framework_failure (grep -m2 x >/dev/null && head -n1) 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