summaryrefslogtreecommitdiffstats
path: root/usr.bin/grep/grep.c
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2005-04-03 19:12:40 +0000
committerotto <otto@openbsd.org>2005-04-03 19:12:40 +0000
commit30c8a262fdc7e6599b02771b688edce75570b0ce (patch)
tree156da867ec6165eab47eb3cceb02d9d1dc500122 /usr.bin/grep/grep.c
parentReport quotas > 10GB in a readable way. Diff based on PR 2836. (diff)
downloadwireguard-openbsd-30c8a262fdc7e6599b02771b688edce75570b0ce.tar.xz
wireguard-openbsd-30c8a262fdc7e6599b02771b688edce75570b0ce.zip
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/grep.c')
-rw-r--r--usr.bin/grep/grep.c17
1 files changed, 11 insertions, 6 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);