From 0f996b20a4acb478a13ddb746bc9ee1b321b9bb5 Mon Sep 17 00:00:00 2001 From: dim Date: Tue, 9 Aug 2016 18:49:19 +0000 Subject: [PATCH] MFC r270132 (by gabor): - Do not look for more matching lines if -L is specified Submitted by: eadler (based on) MFC r296799 (by ian): Fix a bug in bsdgrep that caused the program to hang in a tight loop for some combinations of command line options and search patterns. The code was examining regexec flags looking for a regcomp flag value. The fix is to look in the struct field where the decoded regcomp flag was stored when the regex was compiled. With this fix, it's possible to build WITHOUT_GNU_GREP_COMPAT and WITH_BSDGREP and have a usable GPL-free grep (which of course lacks gnugrep extensions). It now passes the kyua tests except for one test that requires the -z/--null-data gnu extension, and one test involving outputting context lines across multiple files which appears to sometimes output an extra delimiter line ("--") between matches (a rather obscure failure of a rather obscure feature, so bsdgrep should be generally usable now). MFC r303676: Fix a segfault in bsdgrep when parsing the invalid extended regexps "?" or "+" (these are invalid, because there is no preceding operand). When bsdgrep attempts to emulate GNU grep in discarding and ignoring the invalid ? or + operators, some later logic in tre_compile_fast() goes beyond the end of the buffer, leading to a crash. Fix this by bailing out, and reporting a bad pattern instead. Reported by: Steve Kargl --- usr.bin/grep/regex/glue.h | 2 +- usr.bin/grep/regex/tre-fastmatch.c | 2 +- usr.bin/grep/util.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/usr.bin/grep/regex/glue.h b/usr.bin/grep/regex/glue.h index 2fea4fd542c..0c54e98bd79 100644 --- a/usr.bin/grep/regex/glue.h +++ b/usr.bin/grep/regex/glue.h @@ -50,7 +50,7 @@ typedef enum { STR_WIDE, STR_BYTE, STR_MBS, STR_USER } tre_str_type_t; if ((long long)pmatch[0].rm_eo - pmatch[0].rm_so < 0) \ return REG_NOMATCH; \ ret = fn; \ - for (unsigned i = 0; (!(eflags & REG_NOSUB) && (i < nmatch)); i++)\ + for (unsigned i = 0; (!preg->nosub && (i < nmatch)); i++) \ { \ pmatch[i].rm_so += offset; \ pmatch[i].rm_eo += offset; \ diff --git a/usr.bin/grep/regex/tre-fastmatch.c b/usr.bin/grep/regex/tre-fastmatch.c index 0881c557ecf..08e17c79b8c 100644 --- a/usr.bin/grep/regex/tre-fastmatch.c +++ b/usr.bin/grep/regex/tre-fastmatch.c @@ -621,7 +621,7 @@ tre_compile_fast(fastmatch_t *fg, const tre_char_t *pat, size_t n, case TRE_CHAR('+'): case TRE_CHAR('?'): if ((cflags & REG_EXTENDED) && (i == 0)) - continue; + goto badpat; else if ((cflags & REG_EXTENDED) ^ !escaped) STORE_CHAR; else diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index 3ec12fa7520..f3cf05f0411 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -336,7 +336,7 @@ procline(struct str *l, int nottext) } /* One pass if we are not recording matches */ - if (!wflag && ((color == NULL && !oflag) || qflag || lflag)) + if (!wflag && ((color == NULL && !oflag) || qflag || lflag || Lflag)) break; if (st == (size_t)pmatch.rm_so) -- 2.45.0