Subject: bug#37716: closed (Re: bug#37716: grep exit status with -L redirected to /dev/null)

From: [email-masked] (GNU bug Tracking System)
Date: Sun, 13 Oct 2019 01:34:03 +0000

Your bug report

#37716: grep exit status with -L redirected to /dev/null

which was filed against the grep package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
37716: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=37716
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#37716: grep exit status with -L redirected to /dev/null
From: Paul Eggert <[email-masked]>
To: Adam Sampson <[email-masked]>
Cc: Paul Jackson <[email-masked]>, [email-masked]
Date: Sat, 12 Oct 2019 18:33:23 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

Thanks for reporting that bug. To fix it, I installed the attached patch into 
grep master on Savannah.
>From 69a947f97e3389f409946d990820af4b00095fa6 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sat, 12 Oct 2019 18:31:04 -0700
Subject: [PATCH] =?UTF-8?q?grep:=20fix=20=E2=80=98grep=20-L=20...=20>/dev/?=
 =?UTF-8?q?null=E2=80=99=20bug?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problem reported by Adam Sampson (Bug#37716).
* NEWS: Mention this.
* src/grep.c (grepdesc): Don’t assume that stdout being /dev/null
means list_files == LISTFILES_NONE.
(main): Do not change list_files merely because stdout is /dev/null.
* tests/skip-read: Test for this bug.
---
 NEWS            | 6 ++++++
 src/grep.c      | 4 ++--
 tests/skip-read | 1 +
 3 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 5cb33cd..9ce65b3 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,12 @@ GNU grep NEWS                                    -*- outline -*-
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** Bug fixes
+
+  The exit status of 'grep -L' is no longer incorrect when standard
+  output is /dev/null.
+  [Bug#37716 introduced in grep 3.2]
+
 ** Build-related
 
   The build procedure no longer relies on any already-built src/grep
diff --git a/src/grep.c b/src/grep.c
index 4b6b465..6f35f26 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1864,7 +1864,7 @@ grepdesc (int desc, bool command_line)
 
   status = !count == !(list_files == LISTFILES_NONMATCHING);
 
-  if (list_files == LISTFILES_NONE)
+  if (list_files == LISTFILES_NONE || dev_null_output)
     finalize_input (desc, &st, ineof);
   else if (status == 0)
     {
@@ -2827,7 +2827,7 @@ main (int argc, char **argv)
 
   /* POSIX says -c, -l and -q are mutually exclusive.  In this
      implementation, -q overrides -l and -L, which in turn override -c.  */
-  if (exit_on_match | dev_null_output)
+  if (exit_on_match)
     list_files = LISTFILES_NONE;
   if ((exit_on_match | dev_null_output) || list_files != LISTFILES_NONE)
     {
diff --git a/tests/skip-read b/tests/skip-read
index 3c32c48..2a17334 100755
--- a/tests/skip-read
+++ b/tests/skip-read
@@ -17,6 +17,7 @@ for opts in '-m0 y' '-f /dev/null' '-v ""'; do
     done
     eval grep -L $opts $matcher /dev/null > out || fail=1
     compare exp out || fail=1
+    eval grep -L $opts $matcher /dev/null > /dev/null || fail=1
   done
 done
 
-- 
2.17.1

Subject: grep exit status with -L redirected to /dev/null
From: Adam Sampson <[email-masked]>
To: [email-masked]
Date: Sat, 12 Oct 2019 15:22:05 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi grep maintainers,

This behaviour, with grep 3.3 built from source, is a bit surprising:

$ echo hello >thing
$ grep -L goodbye thing && echo OK
thing
OK
$ grep -L goodbye thing >/dev/null && echo OK
$

That is, when using -L, redirecting stdout to /dev/null has the same
effect on the exit code as specifying -q (the behaviour with -q is
described in the "Exit Status" section of the manual). This is a bit
awkward if you're using grep in a script, because you don't necessarily
have any control over whether stdout is /dev/null or not.

Cheers,

-- 
Adam Sampson <[email-masked]>                         <http://offog.org/>