Thanks for the bug report and fix, Jaroslav. And thanks, Norihiro, for the test
case; I think I independently came up with something similar to your grep.c fix
in my earlier patches today and so I expect that part of your changes are no
longer needed. I installed the attached combined patch for this bug and am
marking it as done.
>From 25402655017e4281cc443ee0c1c9f2f388f14af5 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Wed, 30 Dec 2015 23:22:36 -0800
Subject: [PATCH] grep: -c should keep counting after binary data
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Problem and fix reported by Jaroslav Å karvada, and test case
reported by Norihiro Tanaka, in: http://bugs.gnu.org/22028
* NEWS: Document this.
* src/grep.c (grep): Don't stop counting merely because nulls seen.
* tests/pcre-count: New file.
* tests/Makefile.am (TESTS): Add it.
---
NEWS | 3 +++
src/grep.c | 3 ++-
tests/Makefile.am | 1 +
tests/pcre-count | 23 +++++++++++++++++++++++
4 files changed, 29 insertions(+), 1 deletion(-)
create mode 100755 tests/pcre-count
diff --git a/NEWS b/NEWS
index a14597f..4bf408c 100644
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,9 @@ GNU grep NEWS -*- outline -*-
an encoding error in FOO before generating output for FOO.
[bug introduced in grep-2.21]
+ grep -c no longer stops counting when finding binary data.
+ [bug introduced in grep-2.21]
+
grep -oP is no longer susceptible to an infinite loop when processing
invalid UTF8 just before a match.
[bug introduced in grep-2.22]
diff --git a/src/grep.c b/src/grep.c
index e059a46..06f315d 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1397,7 +1397,8 @@ grep (int fd, struct stat const *st)
has_nulls = true;
if (binary_files == WITHOUT_MATCH_BINARY_FILES)
return 0;
- done_on_match = out_quiet = true;
+ if (!count_matches)
+ done_on_match = out_quiet = true;
nul_zapper = eol;
skip_nuls = skip_empty_lines;
}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f1b8c43..e8b11b5 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -106,6 +106,7 @@ TESTS = \
pcre \
pcre-abort \
pcre-context \
+ pcre-count \
pcre-infloop \
pcre-invalid-utf8-infloop \
pcre-invalid-utf8-input \
diff --git a/tests/pcre-count b/tests/pcre-count
new file mode 100755
index 0000000..78e1c7c
--- /dev/null
+++ b/tests/pcre-count
@@ -0,0 +1,23 @@
+#! /bin/sh
+# grep -P / grep -Pc are inconsistent results
+# This bug affected grep versions 2.21 through 2.22.
+#
+# Copyright (C) 2015 Free Software Foundation, Inc.
+#
+# Copying and distribution of this file, with or without modification,
+# are permitted in any medium without royalty provided the copyright
+# notice and this notice are preserved.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
+require_pcre_
+
+fail=0
+
+printf 'a\n%032768d\nb\x0\n%032768d\na\n' 0 0 > in
+
+LC_ALL=C grep -P 'a' in | wc -l > exp
+
+LC_ALL=C grep -Pc 'a' in > out || fail=1
+compare exp out || fail=1
+
+Exit $fail
--
2.5.0