summaryrefslogtreecommitdiff
path: root/usr.bin/grep
diff options
context:
space:
mode:
authorOtto Moerbeek <otto@cvs.openbsd.org>2005-04-03 19:12:41 +0000
committerOtto Moerbeek <otto@cvs.openbsd.org>2005-04-03 19:12:41 +0000
commit90dbf46dcc517309179b2d183d29024aaa424174 (patch)
tree9e86f4d913b3c72ef30c4054f5f27500133a1f3a /usr.bin/grep
parent2a84335088b17863bfb6ba7b15f23c512855adbb (diff)
Protect begin and end of word markers added to the pattern when
using the -w option with parentheses, to avoid operators in the expressions binding to the markers. Compare [[:<:]]foo|bar[[:>:]] and [[:<:]](foo|bar)[[:>:]]. Problem spotted by aaron@; ok millert@ aaron@ jaredy@
Diffstat (limited to 'usr.bin/grep')
-rw-r--r--usr.bin/grep/grep.c17
-rw-r--r--usr.bin/grep/util.c9
2 files changed, 18 insertions, 8 deletions
diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c
index f416f15e9a2..4c64b94afe5 100644
--- a/usr.bin/grep/grep.c
+++ b/usr.bin/grep/grep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grep.c,v 1.31 2004/10/03 19:23:02 otto Exp $ */
+/* $OpenBSD: grep.c,v 1.32 2005/04/03 19:12:40 otto Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -175,16 +175,21 @@ add_pattern(char *pat, size_t len)
--len;
/* pat may not be NUL-terminated */
if (wflag && !Fflag) {
- int bol = 0, eol = 0;
+ int bol = 0, eol = 0, extra;
if (pat[0] == '^')
bol = 1;
if (pat[len - 1] == '$')
eol = 1;
- pattern[patterns] = grep_malloc(len + 15);
- snprintf(pattern[patterns], len + 15, "%s[[:<:]]%.*s[[:>:]]%s",
- bol ? "^" : "", (int)len - bol - eol, pat + bol,
+ extra = Eflag ? 2 : 4;
+ pattern[patterns] = grep_malloc(len + 15 + extra);
+ snprintf(pattern[patterns], len + 15 + extra,
+ "%s[[:<:]]%s%.*s%s[[:>:]]%s",
+ bol ? "^" : "",
+ Eflag ? "(" : "\\(",
+ (int)len - bol - eol, pat + bol,
+ Eflag ? ")" : "\\)",
eol ? "$" : "");
- len += 14;
+ len += 14 + extra;
} else {
pattern[patterns] = grep_malloc(len + 1);
memcpy(pattern[patterns], pat, len);
diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c
index f168fe5ae90..1f93e9ef632 100644
--- a/usr.bin/grep/util.c
+++ b/usr.bin/grep/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.29 2004/10/03 19:23:02 otto Exp $ */
+/* $OpenBSD: util.c,v 1.30 2005/04/03 19:12:40 otto Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -302,7 +302,12 @@ fastcomp(fastgrep_t *fg, const char *pattern)
}
/* Remove enclosing [[:<:]] and [[:>:]] (word match). */
- if (fg->patternLen >= 14 &&
+ if (wflag) {
+ /* basic re's use \( \), extended re's ( ) */
+ int extra = Eflag ? 1 : 2;
+ fg->patternLen -= 14 + 2 * extra;
+ fg->wmatch = 7 + extra;
+ } else if (fg->patternLen >= 14 &&
strncmp(pattern + fg->bol, "[[:<:]]", 7) == 0 &&
strncmp(pattern + fg->bol + fg->patternLen - 7, "[[:>:]]", 7) == 0) {
fg->patternLen -= 14;