aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2021-01-23 03:00:35 -0600
committerRob Landley <rob@landley.net>2021-01-23 03:00:35 -0600
commit76d3160cfabe15568f006ad530c105b9ad6fdcdd (patch)
treec46cab49a8a66554d32da80fbd4a2c3d46f9fb2a
parent054d82ff46e236a7238c73d3dadfe446a6de4ff3 (diff)
downloadtoybox-76d3160cfabe15568f006ad530c105b9ad6fdcdd.tar.gz
Fix grep bug where -f /dev/null added "" regex matching everything,
and address TODO where -z was still splitting patterns on \n
-rwxr-xr-xtests/grep.test6
-rw-r--r--toys/posix/grep.c26
2 files changed, 19 insertions, 13 deletions
diff --git a/tests/grep.test b/tests/grep.test
index e3753c2c..c6171112 100755
--- a/tests/grep.test
+++ b/tests/grep.test
@@ -85,7 +85,7 @@ testing "-r dir" "grep -r one sub | sort" "sub/one:one\nsub/two:one\n" \
"" ""
rm -rf sub
-# -x exact match trumps -F's "empty string matches whole line" behavior
+# -x exact match overrides -F's "empty string matches whole line" behavior
testing "-Fx ''" "grep -Fx '' input" "" "one one one\n" ""
testing "-F ''" "grep -F '' input" "one one one\n" "one one one\n" ""
testing "-F -e blah -e ''" "grep -F -e blah -e '' input" "one one one\n" \
@@ -138,7 +138,7 @@ testing '' "grep -o -e '' -e two" "two\ntwo\n" "" \
"one two three\none two\none\n"
echo "one\ntwo\nthree" > test
-testing "-l trumps -C" "grep -l -C1 two test input" "test\ninput\n" \
+testing "-l overrides -C" "grep -l -C1 two test input" "test\ninput\n" \
"three\ntwo\none\n" ""
rm test
@@ -197,3 +197,5 @@ 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" ""
+testing "-f /dev/null" "grep -f /dev/null" "" "" "hello\n"
+testing "-z with \n in pattern" "grep -f input" "hi\nthere\n" "i\nt" "hi\nthere"
diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index fce8d564..2ddb6b93 100644
--- a/toys/posix/grep.c
+++ b/toys/posix/grep.c
@@ -372,13 +372,24 @@ static void parse_regex(void)
// exit to free. Not supporting nofork for this command any time soon.)
al = TT.f ? TT.f : TT.e;
while (al) {
- if (TT.f) s = ss = xreadfile(al->arg, 0, 0);
- else s = ss = al->arg;
+ if (TT.f) {
+ if (!*(s = ss = xreadfile(al->arg, 0, 0))) {
+ free(ss);
+ s = 0;
+ }
+ } else s = ss = al->arg;
+
+ // Advance, when we run out of -f switch to -e.
+ al = al->next;
+ if (!al && TT.f) {
+ TT.f = 0;
+ al = TT.e;
+ }
+ if (!s) continue;
// 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');
+ ss = FLAG(z) ? 0 : strchr(s, '\n');
if (ss) *(ss++) = 0;
new = xmalloc(sizeof(struct arg_list));
new->next = list;
@@ -386,13 +397,6 @@ static void parse_regex(void)
list = new;
s = ss;
} while (ss && *s);
-
- // Advance, when we run out of -f switch to -e.
- al = al->next;
- if (!al && TT.f) {
- TT.f = 0;
- al = TT.e;
- }
}
TT.e = list;