diff options
Diffstat (limited to 'bin/ed/re.c')
-rw-r--r-- | bin/ed/re.c | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/bin/ed/re.c b/bin/ed/re.c index 413011da63f..c81a3ee0518 100644 --- a/bin/ed/re.c +++ b/bin/ed/re.c @@ -1,4 +1,4 @@ -/* $OpenBSD: re.c,v 1.18 2018/06/18 17:08:20 martijn Exp $ */ +/* $OpenBSD: re.c,v 1.19 2018/06/19 12:36:18 martijn Exp $ */ /* $NetBSD: re.c,v 1.14 1995/03/21 09:04:48 cgd Exp $ */ /* re.c: This file contains the regular expression interface routines for @@ -94,10 +94,16 @@ extract_pattern(int delimiter) char *nd; int len; - for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++) { + for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++) switch (*nd) { default: break; + case '[': + if ((nd = parse_char_class(++nd)) == NULL) { + seterrmsg("unbalanced brackets ([])"); + return NULL; + } + break; case '\\': if (*++nd == '\n') { seterrmsg("trailing backslash (\\)"); @@ -105,7 +111,6 @@ extract_pattern(int delimiter) } break; } - } len = nd - ibufp; REALLOC(lhbuf, lhbufsz, len + 1, NULL); memcpy(lhbuf, ibufp, len); @@ -113,3 +118,22 @@ extract_pattern(int delimiter) ibufp = nd; return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf; } + + +/* parse_char_class: expand a POSIX character class */ +static char * +parse_char_class(char *s) +{ + int c, d; + + if (*s == '^') + s++; + if (*s == ']') + s++; + for (; *s != ']' && *s != '\n'; s++) + if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) + for (s++, c = *++s; *s != ']' || c != d; s++) + if ((c = *s) == '\n') + return NULL; + return (*s == ']') ? s : NULL; +} |