summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2015-12-30 23:22:36 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2015-12-30 23:23:30 -0800
commit413fe0e603740b9cdeb49d61aa789f7254e13d18 (patch)
tree507e6771bb2b898de7f8b24dcbabff15d944e5f0
parent5827733ce621bb169afc4f9220cc4624124fc7ba (diff)
downloadgrep-413fe0e603740b9cdeb49d61aa789f7254e13d18.tar.gz
grep: -c should keep counting after binary data
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.
-rw-r--r--NEWS3
-rw-r--r--src/grep.c3
-rw-r--r--tests/Makefile.am1
-rwxr-xr-xtests/pcre-count23
4 files changed, 29 insertions, 1 deletions
diff --git a/NEWS b/NEWS
index a14597f1..4bf408c8 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 e059a464..06f315d7 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 f1b8c436..e8b11b5a 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 00000000..78e1c7cc
--- /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