On 8/20/21 8:25 AM, Paul Eggert wrote:

(Not that anyone would ever want to *use* plain "grep -f -", except perhaps to file bug reports....)

I discovered a more-artificial case where grep messes up even on modern platforms, namely 'grep -f - -f -' where grep essentially ignores the second '-f -'. I installed the attached to fix that, along with another bug where grep wasn't reporting fclose errors.

I think these bugs are so unlikely and artificial that they're not worth mentioning in NEWS.

As a side effect, this patch might fix the bug#50129 problem on your old platform. Hard to say.
From 5649624925c421427cd4ec0f8a7e332c94532b33 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Sat, 21 Aug 2021 10:44:17 -0700
Subject: [PATCH] =?UTF-8?q?grep:=20avoid=20sticky=20problem=20with=20?=
 =?UTF-8?q?=E2=80=98-f=20-=20-f=20-=E2=80=99?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Inspired by bug#50129 even though this is a different bug.
* src/grep.c (main): For ‘-f -’, use clearerr (stdin) after
reading, so that ‘grep -f - -f -’ reads stdin twice even
when stdin is a tty.  Also, for ‘-f FILE’, report any
I/O error when closing FILE.
---
 src/grep.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/grep.c b/src/grep.c
index 7a33686..b2a0566 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -2477,7 +2477,6 @@ main (int argc, char **argv)
   int matcher = -1;
   int opt;
   int prev_optind, last_recursive;
-  int fread_errno;
   intmax_t default_context;
   FILE *fp;
   exit_failure = EXIT_TROUBLE;
@@ -2648,11 +2647,17 @@ main (int argc, char **argv)
               if (cc == 0)
                 break;
             }
-          fread_errno = errno;
-          if (ferror (fp))
-            die (EXIT_TROUBLE, fread_errno, "%s", optarg);
-          if (fp != stdin)
-            fclose (fp);
+          int err = errno;
+          if (!ferror (fp))
+            {
+              err = 0;
+              if (fp == stdin)
+                clearerr (fp);
+              else if (fclose (fp) != 0)
+                err = errno;
+            }
+          if (err)
+            die (EXIT_TROUBLE, err, "%s", optarg);
           /* Append final newline if file ended in non-newline. */
           if (newkeycc != keycc && keys[newkeycc - 1] != '\n')
             keys[newkeycc++] = '\n';
-- 
2.30.2

Reply via email to