fix pathological cases

This commit is contained in:
Kyryl Melekhin
2021-09-02 22:55:03 +00:00
parent 67c691f95b
commit 45b981b6a4
2 changed files with 30 additions and 7 deletions

13
pike.c
View File

@@ -427,10 +427,10 @@ int re_comp(rcode *prog, const char *re, int nsubs)
#define _return(state) \
{ prog->gen = gen + 1; return state; } \
#define newsub(init) \
#define newsub(init, copy) \
s1 = freesub; \
if (s1) \
freesub = (rsub*)s1->sub[0]; \
{ freesub = (rsub*)s1->sub[0]; copy } \
else \
{ s1 = (rsub*)&nsubs[rsubsize * subidx++]; init } \
@@ -454,12 +454,11 @@ subs[i++] = sub; \
goto next##nn; \
#define save1() \
newsub(for (j = nsubp / 2; j < nsubp; j++) s1->sub[j] = NULL;) \
for (j = 0; j < nsubp / 2; j++) \
s1->sub[j] = sub->sub[j]; \
newsub(for (j = 0; j < nsubp; j++) s1->sub[j] = sub->sub[j];, \
for (j = 0; j < nsubp / 2; j++) s1->sub[j] = sub->sub[j];) \
#define save2() \
newsub(/*nop*/) \
newsub(/*nop*/, /*nop*/) \
for (j = 0; j < nsubp; j++) \
s1->sub[j] = sub->sub[j]; \
@@ -586,7 +585,7 @@ int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp)
nlistidx = 0;
if (!matched) {
jmp_start:
newsub(for (i = 1; i < nsubp; i++) s1->sub[i] = NULL;)
newsub(for (i = 1; i < nsubp; i++) s1->sub[i] = NULL;, /*nop*/)
s1->ref = 1;
s1->sub[0] = _sp;
addthread(1, clist, clistidx, insts, s1)

24
test.sh
View File

@@ -106,6 +106,14 @@ qwerty.*$
(aaaa|aaa|a){3,4}
(a)(a)
(a){2}
(a|bcdef|g|ab|c|d|e|efg|fg)*
(.*) (.*) (.*) (.*) (.*)
(.+?)(.+?)
(a?)(a?)(a?)aaa
(ab)?(ab)?(ab)?aaa
(.+)(.+)
a(?:b|c|d){4,5}(.)
(?:c|d)(?:)(?:a(?:)(?:b)(?:b(?:))(?:b(?:)(?:b)))
"
input="\
abcdef
@@ -213,6 +221,14 @@ aaaaaa
aaaaaaaaaa
aaaa
aaaa
abcdefg
a c d ee fff
abcd
aaa
abaaa
abcd
acdbcdbe
cabbbb
"
expect="\
(0,3)
@@ -320,6 +336,14 @@ expect="\
(0,10)(9,10)
(0,2)(0,1)(1,2)
(0,2)(1,2)
(0,7)(6,7)
(0,13)(0,2)(3,4)(5,6)(7,9)(10,13)
(0,2)(0,1)(1,2)
(0,3)(0,0)(0,0)(0,0)
(0,5)(0,2)(?,?)(?,?)
(0,4)(0,3)(3,4)
(0,7)(6,7)
(0,6)
(0,0)
"