From 90dbf46dcc517309179b2d183d29024aaa424174 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Sun, 3 Apr 2005 19:12:41 +0000 Subject: 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@ --- usr.bin/grep/grep.c | 17 +++++++++++------ usr.bin/grep/util.c | 9 +++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) (limited to 'usr.bin/grep') 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; -- cgit v1.2.3