Package: grep / 2.27-2

upstream-grep-fix-bug-dev-null-output.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
From 7ad47abbcb070946000771a829b51224720b8cef Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Tue, 27 Dec 2016 11:16:32 -0800
Subject: [PATCH] grep: fix bug with '... | grep pat >> /dev/null'
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problem reported by Benno Fünfstück (Bug#25283).
* NEWS: Document this.
* src/grep.c (drain_input) [SPLICE_F_MOVE]:
Don't assume /dev/null is always acceptable output to splice.
* tests/grep-dev-null-out: Test for the bug.
---
 NEWS                    |  7 ++++---
 src/grep.c              | 14 +++++++++-----
 tests/grep-dev-null-out |  2 ++
 3 files changed, 15 insertions(+), 8 deletions(-)

--- a/src/grep.c
+++ b/src/grep.c
@@ -1737,11 +1737,15 @@
     {
 #ifdef SPLICE_F_MOVE
       /* Should be faster, since it need not copy data to user space.  */
-      while ((nbytes = splice (fd, NULL, STDOUT_FILENO, NULL,
-                               INITIAL_BUFSIZE, SPLICE_F_MOVE)))
-        if (nbytes < 0)
-          return false;
-      return true;
+      nbytes = splice (fd, NULL, STDOUT_FILENO, NULL,
+                       INITIAL_BUFSIZE, SPLICE_F_MOVE);
+      if (0 <= nbytes || errno != EINVAL)
+        {
+          while (0 < nbytes)
+            nbytes = splice (fd, NULL, STDOUT_FILENO, NULL,
+                             INITIAL_BUFSIZE, SPLICE_F_MOVE);
+          return nbytes == 0;
+        }
 #endif
     }
   while ((nbytes = safe_read (fd, buffer, bufalloc)))
--- a/tests/grep-dev-null-out
+++ b/tests/grep-dev-null-out
@@ -8,4 +8,6 @@
 ${AWK-awk} 'BEGIN {while (1) print "x"}' </dev/null |
   returns_ 124 timeout 1 grep x >/dev/null || fail=1
 
+echo abc | grep b >>/dev/null || fail=1
+
 Exit $fail