From 76d3160cfabe15568f006ad530c105b9ad6fdcdd Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 23 Jan 2021 03:00:35 -0600 Subject: Fix grep bug where -f /dev/null added "" regex matching everything, and address TODO where -z was still splitting patterns on \n --- toys/posix/grep.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'toys/posix') 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; -- cgit v1.2.3