From 45b981b6a4b19ce9d16ab7735ccb02fa646b40b3 Mon Sep 17 00:00:00 2001 From: Kyryl Melekhin Date: Thu, 2 Sep 2021 22:55:03 +0000 Subject: [PATCH] fix pathological cases --- pike.c | 13 ++++++------- test.sh | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/pike.c b/pike.c index 1801ab1..37f659a 100644 --- a/pike.c +++ b/pike.c @@ -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) diff --git a/test.sh b/test.sh index 6486070..443e2f5 100755 --- a/test.sh +++ b/test.sh @@ -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) "