Subject: bug#22461: closed (Re: problem with "Binary file messages" in latest snapshot)

From: [email-masked] (GNU bug Tracking System)
Date: Mon, 01 Feb 2016 07:32:02 +0000

Your bug report

#22461: problem with "Binary file messages" in latest snapshot

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].

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

Subject: Re: problem with "Binary file messages" in latest snapshot
From: Paul Eggert <[email-masked]>
To: [email-masked]
Date: Sun, 31 Jan 2016 23:31:18 -0800
List-Id: <debbugs-submit.debbugs.gnu.org>

I installed the attached patch, which should fix the bug, and am closing this.
>From 1d6609c299d2a51747c9bc9e82a399d53c54f8ea Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sun, 31 Jan 2016 23:29:01 -0800
Subject: [PATCH] Omit excess "Binary file ... matches"

Problem reported in: http://bugs.gnu.org/22461
* src/grep.c (grep): Don't report "Binary file ... matches"
merely because the file contained both matches and binary data.
Insist that the binary data contained a match.
* tests/null-byte: Add a test for this.
---
 src/grep.c      | 16 +++++++++++-----
 tests/null-byte |  5 +++++
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/grep.c b/src/grep.c
index 10aabf9..73c3651 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1373,7 +1373,11 @@ grep (int fd, struct stat const *st)
   char nul_zapper = '\0';
   bool done_on_match_0 = done_on_match;
   bool out_quiet_0 = out_quiet;
-  bool has_nulls = false;
+
+  /* The value of NLINES when nulls were first deduced in the input;
+     this is not necessarily the same as the number of matching lines
+     before the first null.  -1 if no input nulls have been deduced.  */
+  intmax_t nlines_first_null = -1;
 
   if (! reset (fd, st))
     return 0;
@@ -1400,15 +1404,15 @@ grep (int fd, struct stat const *st)
 
   for (bool firsttime = true; ; firsttime = false)
     {
-      if (!has_nulls && eol && binary_files != TEXT_BINARY_FILES
+      if (nlines_first_null < 0 && eol && binary_files != TEXT_BINARY_FILES
           && (buf_has_nulls (bufbeg, buflim - bufbeg)
               || (firsttime && file_must_have_nulls (buflim - bufbeg, fd, st))))
         {
-          has_nulls = true;
           if (binary_files == WITHOUT_MATCH_BINARY_FILES)
             return 0;
           if (!count_matches)
             done_on_match = out_quiet = true;
+          nlines_first_null = nlines;
           nul_zapper = eol;
           skip_nuls = skip_empty_lines;
         }
@@ -1445,7 +1449,8 @@ grep (int fd, struct stat const *st)
             nlines += grepbuf (beg, lim);
           if (pending)
             prpending (lim);
-          if ((!outleft && !pending) || (nlines && done_on_match))
+          if ((!outleft && !pending)
+              || (done_on_match && MAX (0, nlines_first_null) < nlines))
             goto finish_grep;
         }
 
@@ -1490,7 +1495,8 @@ grep (int fd, struct stat const *st)
  finish_grep:
   done_on_match = done_on_match_0;
   out_quiet = out_quiet_0;
-  if ((has_nulls || encoding_error_output) && !out_quiet && nlines != 0)
+  if (!out_quiet && (encoding_error_output
+                     || (0 <= nlines_first_null && nlines_first_null < nlines)))
     {
       printf (_("Binary file %s matches\n"), filename);
       if (line_buffered)
diff --git a/tests/null-byte b/tests/null-byte
index 44dad92..9a76887 100755
--- a/tests/null-byte
+++ b/tests/null-byte
@@ -51,4 +51,9 @@ for left in '' a '#' '\0'; do
   done
 done
 
+(echo xxx && yes yyy | sed 100000q && printf '\0') >in || framework_failure_
+echo xxx >exp || framework_failure_
+grep xxx in >out || fail=1
+compare exp out || fail=1
+
 Exit $fail
-- 
2.5.0

Subject: problem with "Binary file messages" in latest snapshot
From: Paul Eggert <[email-masked]>
To: grep mailing list <[email-masked]>
Date: Mon, 25 Jan 2016 01:19:42 -0800
List-Id: <debbugs-submit.debbugs.gnu.org>

I ran into this problem when using 'grep' to search through GNU Emacs installed 
files. Here's how to reproduce the problem:

$ (echo xxx && yes yyy | sed 100000q && printf '\0') >big
$ grep xxx big
xxx
Binary file big matches

The last line should not be output. I'll look into fixing this.