This came up trying to build kernels. Worked around for now by
https://android-review.googlesource.com/c/kernel/build/+/1558655. POSIX
says nothing about this, but the GNU grep man page explicitly mentions
this behavior.

Bug: https://issuetracker.google.com/178191979
---
 tests/grep.test   |  8 ++++++++
 toys/posix/grep.c | 25 +++++++++++++++----------
 2 files changed, 23 insertions(+), 10 deletions(-)
From cf861263a665a23566352e4aae2cfeea41792227 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Fri, 22 Jan 2021 16:20:44 -0800
Subject: [PATCH] grep: `-f empty-file` matches nothing, not everything.

This came up trying to build kernels. Worked around for now by
https://android-review.googlesource.com/c/kernel/build/+/1558655. POSIX
says nothing about this, but the GNU grep man page explicitly mentions
this behavior.

Bug: https://issuetracker.google.com/178191979
---
 tests/grep.test   |  8 ++++++++
 toys/posix/grep.c | 25 +++++++++++++++----------
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/tests/grep.test b/tests/grep.test
index e3753c2c..23b0ad87 100755
--- a/tests/grep.test
+++ b/tests/grep.test
@@ -197,3 +197,11 @@ testing "-Fx" "grep -Fx h input" "h\n" \
   "missing\nH\nthis is hello\nthis is world\nh\nmissing" ""
 testing "-Fix" "grep -Fix h input" "H\nh\n" \
   "missing\nH\nthis is HELLO\nthis is WORLD\nh\nmissing" ""
+
+rm -f patternfile
+touch patternfile
+testing "-f empty file" "grep -f patternfile input" "" "test\n" ""
+echo > patternfile
+testing "-f empty string" "grep -f patternfile input" "test\n" "test\n" ""
+echo "est" > patternfile
+testing "-f" "grep -f patternfile input" "test\n" "test\n" ""
diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index fce8d564..758d6422 100644
--- a/toys/posix/grep.c
+++ b/toys/posix/grep.c
@@ -375,17 +375,22 @@ static void parse_regex(void)
     if (TT.f) s = ss = xreadfile(al->arg, 0, 0);
     else s = ss = al->arg;
 
-    // Split lines at \n, add individual lines to new list.
-    do {
+    if (TT.f && !*s) {
+      // An empty -f *file* matches nothing, but a -f file containing an
+      // empty line, or an empty -F *argument* both match everything.
+    } else {
+      // Split lines at \n, add individual lines to new list.
+      do {
 // TODO: NUL terminated input shouldn't split -e at \n
-      ss = strchr(s, '\n');
-      if (ss) *(ss++) = 0;
-      new = xmalloc(sizeof(struct arg_list));
-      new->next = list;
-      new->arg = s;
-      list = new;
-      s = ss;
-    } while (ss && *s);
+        ss = strchr(s, '\n');
+        if (ss) *(ss++) = 0;
+        new = xmalloc(sizeof(struct arg_list));
+        new->next = list;
+        new->arg = s;
+        list = new;
+        s = ss;
+      } while (ss && *s);
+    }
 
     // Advance, when we run out of -f switch to -e.
     al = al->next;
-- 
2.30.0.280.ga3ce27912f-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to