summaryrefslogtreecommitdiffstats
path: root/lib/libc/regex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/regex')
-rw-r--r--lib/libc/regex/engine.c4
-rw-r--r--lib/libc/regex/re_format.710
-rw-r--r--lib/libc/regex/regcomp.c29
-rw-r--r--lib/libc/regex/regex.36
4 files changed, 36 insertions, 13 deletions
diff --git a/lib/libc/regex/engine.c b/lib/libc/regex/engine.c
index 589bb9d..436370d 100644
--- a/lib/libc/regex/engine.c
+++ b/lib/libc/regex/engine.c
@@ -157,7 +157,7 @@ matcher(struct re_guts *g,
int i;
struct match mv;
struct match *m = &mv;
- const char *dp;
+ const char *dp = NULL;
const sopno gf = g->firststate+1; /* +1 for OEND */
const sopno gl = g->laststate;
const char *start;
@@ -244,7 +244,7 @@ matcher(struct re_guts *g,
ZAPSTATE(&m->mbs);
/* Adjust start according to moffset, to speed things up */
- if (g->moffset > -1)
+ if (dp != NULL && g->moffset > -1)
start = ((dp - g->moffset) < start) ? start : dp - g->moffset;
SP("mloop", m->st, *start);
diff --git a/lib/libc/regex/re_format.7 b/lib/libc/regex/re_format.7
index 05b1494..b3f9561 100644
--- a/lib/libc/regex/re_format.7
+++ b/lib/libc/regex/re_format.7
@@ -314,10 +314,10 @@ compatible with but not specified by
.St -p1003.2 ,
and should be used with
caution in software intended to be portable to other systems.
-The additional word delimiters
+The additional word delimiters
.Ql \e<
and
-.Ql \e>
+.Ql \e>
are provided to ease compatibility with traditional
.Xr svr4 4
systems but are not portable and should be avoided.
@@ -392,10 +392,12 @@ and
.Ql ?\&
are ordinary characters, and their functionality
can be expressed using bounds
-.No ( Ql {1,}
+.Po
+.Ql {1,}
or
.Ql {0,1}
-respectively).
+respectively
+.Pc .
Also note that
.Ql x+
in modern REs is equivalent to
diff --git a/lib/libc/regex/regcomp.c b/lib/libc/regex/regcomp.c
index a01bb95..2f2d827 100644
--- a/lib/libc/regex/regcomp.c
+++ b/lib/libc/regex/regcomp.c
@@ -192,6 +192,7 @@ regcomp(regex_t * __restrict preg,
struct parse *p = &pa;
int i;
size_t len;
+ size_t maxlen;
#ifdef REDEBUG
# define GOODFLAGS(f) (f)
#else
@@ -213,7 +214,23 @@ regcomp(regex_t * __restrict preg,
g = (struct re_guts *)malloc(sizeof(struct re_guts));
if (g == NULL)
return(REG_ESPACE);
+ /*
+ * Limit the pattern space to avoid a 32-bit overflow on buffer
+ * extension. Also avoid any signed overflow in case of conversion
+ * so make the real limit based on a 31-bit overflow.
+ *
+ * Likely not applicable on 64-bit systems but handle the case
+ * generically (who are we to stop people from using ~715MB+
+ * patterns?).
+ */
+ maxlen = ((size_t)-1 >> 1) / sizeof(sop) * 2 / 3;
+ if (len >= maxlen) {
+ free((char *)g);
+ return(REG_ESPACE);
+ }
p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
+ assert(p->ssize >= len);
+
p->strip = (sop *)malloc(p->ssize * sizeof(sop));
p->slen = 0;
if (p->strip == NULL) {
@@ -1405,8 +1422,8 @@ static void
findmust(struct parse *p, struct re_guts *g)
{
sop *scan;
- sop *start;
- sop *newstart;
+ sop *start = NULL;
+ sop *newstart = NULL;
sopno newlen;
sop s;
char *cp;
@@ -1709,15 +1726,17 @@ computematchjumps(struct parse *p, struct re_guts *g)
if (p->error != 0)
return;
- pmatches = (int*) malloc(g->mlen * sizeof(unsigned int));
+ pmatches = (int*) malloc(g->mlen * sizeof(int));
if (pmatches == NULL) {
g->matchjump = NULL;
return;
}
- g->matchjump = (int*) malloc(g->mlen * sizeof(unsigned int));
- if (g->matchjump == NULL) /* Not a fatal error */
+ g->matchjump = (int*) malloc(g->mlen * sizeof(int));
+ if (g->matchjump == NULL) { /* Not a fatal error */
+ free(pmatches);
return;
+ }
/* Set maximum possible jump for each character in the pattern */
for (mindex = 0; mindex < g->mlen; mindex++)
diff --git a/lib/libc/regex/regex.3 b/lib/libc/regex/regex.3
index ea1ba25..6df2f09 100644
--- a/lib/libc/regex/regex.3
+++ b/lib/libc/regex/regex.3
@@ -420,10 +420,12 @@ it should have been the result from the most recent
using that
.Ft regex_t .
The
-.Fn ( regerror
+.Po
+.Fn regerror
may be able to supply a more detailed message using information
from the
-.Ft regex_t . )
+.Ft regex_t .
+.Pc
The
.Fn regerror
function
OpenPOWER on IntegriCloud